20260417

対象のファイルの規定(デフォルト)のアプリケーションでファイルを開く

対象のファイルの規定(デフォルト)のアプリケーションでファイルを開く

NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

対象のファイルの規定(デフォルト)のアプリケーションでファイルを開く.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005テスト用のサンプルスクリプト
006複数のファイルを
007HTMLファイルをデフォルトのWEBブラウザ
008(HTMLと関連づけられているアプリケーション)で開きます
009
010
011
012バンドルIDを指定(開くアプリケーションを指定)して開きます
013この方法だと発生しない
014
015
016com.cocolog-nifty.quicktimer.icefloe *)
017----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
018use AppleScript version "2.8"
019use scripting additions
020
021set listFilePath to {"/Library/Documentation/License.lpdf/Contents/Resources/English.lproj/License.html", "/Library/Documentation/License.lpdf/Contents/Resources/German.lproj/License.html", "/Library/Documentation/License.lpdf/Contents/Resources/French.lproj/License.html", "/Library/Documentation/License.lpdf/Contents/Resources/Dutch.lproj/License.html", "/Library/Documentation/License.lpdf/Contents/Resources/Italian.lproj/License.html", "/Library/Documentation/License.lpdf/Contents/Resources/Japanese.lproj/License.html", "/Library/Documentation/License.lpdf/Contents/Resources/Spanish.lproj/License.html"} as list
022
023#エイリアスのリストを作成する
024#空のリスト 
025set listAliasFilePath to {} as list
026
027#テキストファイルパスを順番に
028repeat with itemFilePath in listFilePath
029   #エイリアスにして
030   set itemAlisFilePath to (POSIX file itemFilePath) as alias
031   #リストに追加していく
032   set end of listAliasFilePath to itemAlisFilePath
033end repeat
034
035tell application "Finder"
036   #Finderでエイリアスリストにしておく
037   set listAliasFilePath to listAliasFilePath as alias list
038end tell
039
040#エイリアスを順番に処理
041repeat with itemAliasFilePath in listAliasFilePath
042   #クラスをエイリアスで確定させて
043   set aliasFilePath to itemAliasFilePath as alias
044   #INFO FORコマンドでプロパティを取得して
045   tell file aliasFilePath
046      set recordInfoFor to (info for) as record
047   end tell
048   #UTIを取得します
049   set strUTI to (type identifier of recordInfoFor) as text
050   
051   #UTI=DYNから始まる場合は、launchserviceにデフォルトのアプリケーションが指定されていない事になるので
052   if strUTI starts with "dyn." then
053      #FinderでOPENして、アプリケーション指定のダイアログを出してユーザーに決めさせる
054      tell application "Finder"
055         open file alisFilePath
056      end tell
057      #ダイアログが背面に行かないように前に出す
058      tell application "Finder" to activate
059   else
060      #UTIが取得できる=デフォルトのあぷりケーションが決まっているになるので
061      #デフォルトのアプリケーションを取得して
062      set aliaAppPath to (default application of recordInfoFor) as alias
063      #アプリケーションのプロパティを取得
064      tell file aliaAppPath
065         set recordInfoFor to (info for) as record
066      end tell
067      
068      #アプリケーションのバンドルIDを取得して
069      set strBundleID to (bundle identifier of recordInfoFor) as text
070      
071      #アプリケーションを指定して開く
072      tell application id strBundleID
073         open location aliasFilePath
074      end tell
075   end if
076   
077   
078end repeat
079
080
081return
082
AppleScriptで生成しました