NSBundle

propertyでバンドルID指定する場合の注意事項

今日気がついた

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

property 2.scpt
ソース
001#!/usr/bin/env osascript
002use AppleScript version "2.8"
003use scripting additions
004
005#class型を指定しないでプロパティにバンドルIDを設定する
006property strBundleID : "com.microsoft.edgemac"
007
008tell application id strBundleID to quit
009delay 2
010
011(*
012tell application "Script Editor" -->なぜかスクリプトエディタになる
013   quit
014      --> error number 0
015end tell
016
017*)
AppleScriptで生成しました

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

property as text.scpt
ソース
001#!/usr/bin/env osascript
002use AppleScript version "2.8"
003use scripting additions
004
005#テキスト型を指定してバンドルIDをプロパティに設定すると
006property strBundleID : ("com.microsoft.edgemac") as text
007
008tell application id strBundleID to quit
009delay 2
010(*
011テキスト指定すると意図した形式になる
012tell application "Microsoft Edge"
013   quit
014      --> error number 0
015end tell
016
017*)
AppleScriptで生成しました

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

set.scpt
ソース
001#!/usr/bin/env osascript
002use AppleScript version "2.8"
003use scripting additions
004
005#普通にsetする場合はテキスト型指定してなくても
006set strBundleID to "com.microsoft.edgemac"
007
008tell application id strBundleID to quit
009delay 2
010
011(*
012意図した形式になる
013tell application "Microsoft Edge"
014   quit
015      --> error number 0
016end tell
017
018
019*)
AppleScriptで生成しました

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

property 2.scpt
ソース
001#!/usr/bin/env osascript
002use AppleScript version "2.8"
003use scripting additions
004
005#class型を指定しないでプロパティにバンドルIDを設定する
006property strBundleID : "com.microsoft.edgemac"
007
008log class of strBundleID
009-->(*text*)
010
011tell application id (strBundleID as text) to quit
012delay 2
013
014(*
015classとしてはtextでも
016propertyに指定する場合は明示的にtextClass型で指定しないとダメらしい
017tell application "Microsoft Edge"
018   quit
019      --> error number 0
020end tell
021
022
023*)
AppleScriptで生成しました

| | コメント (0)

バンドルIDからOPENできるファイルのUTIを取得


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

バンドルIDからOPENできるファイルのUTIを取得.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(* 
004バンドルIDから
005アプリケーションが開く事ができる
006UTIの一覧をリストで取得する
007
008com.cocolog-nifty.quicktimer.icefloe *)
009----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
010use AppleScript version "2.8"
011use framework "Foundation"
012use framework "AppKit"
013use framework "UniformTypeIdentifiers"
014use scripting additions
015
016property refMe : a reference to current application
017property strBundleID : "com.apple.Preview"
018
019log doGetUTI()
020
021return
022##################################
023#UTIの取得
024#対象のアプリケーションが開くことができる
025#UTIのリストを取得作成します
026##################################
027to doGetUTI()
028   #アプリケーションのURLを取得
029   #NSバンドルをバンドルIDから取得
030   set ocidAppBundle to refMe's NSBundle's bundleWithIdentifier:(strBundleID)
031   #バンドルが取れない=launchDBの不具合?
032   if ocidAppBundle = (missing value) then
033      #NSバンドル取得できなかった場合
034      set appNSWorkspace to refMe's NSWorkspace's sharedWorkspace()
035      #NSWorkspaceを使って取得する
036      set ocidAppPathURL to appNSWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)
037   else
038      #無駄だけどいちおうFinderにも効いてみる
039      tell application "Finder"
040         try
041            set aliasAppApth to (application file id strBundleID) as alias
042         on error
043            log "アプリケーションが見つかりませんでした"
044            return false
045         end try
046         #えてしてFinderが最強だったりするなこの処理
047         set strAppFilePath to (POSIX path of aliasAppApth) as text
048         set ocidAppFilePathStr to refMe's NSString's stringWithString:(strAppFilePath)
049         set ocidAppFilePath to ocidAppFilePathStr's stringByStandardizingPath()
050         set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAppFilePath) isDirectory:(false)
051      end tell
052      set ocidAppPathURL to ocidAppBundle's bundleURL()
053   end if
054   if ocidAppPathURL = (missing value) then
055      log "アプリケーションが見つかりませんでした"
056      return false
057   end if
058   #NSBundleを定義して
059   set ocidAppBundle to refMe's NSBundle's bundleWithURL:(ocidAppPathURL)
060   set ocidInfoDict to ocidAppBundle's infoDictionary()
061   set ocidDocTypeArray to ocidInfoDict's objectForKey:("CFBundleDocumentTypes")
062   if ocidDocTypeArray = (missing value) then
063      log "CFBundleDocumentTypesが見つかりませんでした"
064      return false
065   else
066      #出力戻し値にするリストの初期化
067      set listUTl to {} as list
068      #対応ドキュメントタイプをリストにしていく
069      repeat with itemDocTypeArray in ocidDocTypeArray
070         set listContentTypes to (itemDocTypeArray's objectForKey:"LSItemContentTypes")
071         if listContentTypes = (missing value) then
072            #拡張子の指定のみの場合
073            set ocidExtension to (itemDocTypeArray's objectForKey:"CFBundleTypeExtensions")
074            set strClassName to ocidExtension's className() as text
075            #子要素の数だけ栗菓子
076            repeat with itemExtension in ocidExtension
077               set strExtension to itemExtension as text
078               #拡張子からUTI生成して
079               set ocidContentTypes to (refMe's UTType's typeWithFilenameExtension:(strExtension))
080               #バンドルIDを取得
081               set strContentTypes to ocidContentTypes's identifier() as text
082               #戻し用のリストに追加していく
083               set end of listUTl to (strContentTypes)
084            end repeat
085         else
086            #収集できたUTIを順番に
087            repeat with itemContentTypes in listContentTypes
088               set strContentTypes to itemContentTypes as text
089               #戻し用のリストに追加していく
090               set end of listUTl to (strContentTypes)
091            end repeat
092         end if
093      end repeat
094   end if
095   #値を戻す
096   return listUTl
097end doGetUTI
AppleScriptで生成しました

| | コメント (0)

AppleScriptからデバイスのCPUタイプの取得


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

CUPタイプの取得.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004CUPタイプでの分岐が必要な場合のCPUの取得
005
006com.cocolog-nifty.quicktimer.icefloe*)
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008use AppleScript version "2.8"
009use framework "Foundation"
010use framework "AppKit"
011use scripting additions
012
013property refMe : a reference to current application
014
015
016####CPUタイプ system info
017set strCPU to CPU type of (system info) as text
018log strCPU as text
019
020####CPUタイプ system attribute
021set numArch to (system attribute "sysa") as integer
022-- 1,    /* Motorola MC68k architecture */
023-- 2,    /* IBM PowerPC architecture */
024-- 10,   /* Intel x86 architecture */
025-- 20    /* Arm architecture */
026if numArch = 20 then
027   log "ARM MACです"
028else if numArch = 10 then
029   log "インテルMacです"
030else
031   log "対象外またはエラー"
032end if
033
034
035set strResponse to (do shell script "/bin/zsh -c '/usr/bin/arch'")
036if strResponse contains "arm" then
037   log "ARM MACです"
038else
039   log "インテルMacです"
040end if
041
042
043set strResponse to (do shell script "/bin/zsh -c '/usr/bin/uname -p'")
044if strResponse is "arm" then
045   log "ARM MACです"
046else
047   log "インテルMacです"
048end if
049
050set strResponse to (do shell script "/bin/zsh -c '/usr/bin/uname -m'")
051if strResponse contains "arm" then
052   log "ARM MACです"
053else
054   log "インテルMacです"
055end if
056
057
058
059#アプリケーションはバンドルで対応を調べる
060set ocidBundle to refMe's NSBundle's mainBundle()
061set ocidArchArray to (ocidBundle's executableArchitectures())
062set boolContainARM to ocidArchArray's containsObject:(refMe's NSBundleExecutableArchitectureARM64 as integer)
063if boolContainARM is true then
064   log "ARM MACで使えます"
065end if
066set boolContainIntel to ocidArchArray's containsObject:(refMe's NSBundleExecutableArchitectureX86_64 as integer)
067if boolContainIntel is true then
068   log "インテルMacで使えます"
069end if
AppleScriptで生成しました

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

CUPタイプの取得.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004CUPタイプでの分岐が必要な場合のCPUの取得
005
006com.cocolog-nifty.quicktimer.icefloe*)
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008use AppleScript version "2.8"
009use framework "Foundation"
010use framework "AppKit"
011use scripting additions
012
013property refMe : a reference to current application
014
015
016
017
018####CPUタイプ system info
019set strCPU to CPU type of (system info) as text
020log strCPU as text
021
022####CPUタイプ system attribute
023set numArch to (system attribute "sysa") as integer
024-- 1,    /* Motorola MC68k architecture */
025-- 2,    /* IBM PowerPC architecture */
026-- 10,   /* Intel x86 architecture */
027-- 20    /* Arm architecture */
028if numArch = 20 then
029   log "ARM MACです"
030else if numArch = 10 then
031   log "インテルMacです"
032else
033   log "対象外またはエラー"
034end if
035
036
037set strResponse to (do shell script "/bin/zsh -c '/usr/bin/arch'")
038if strResponse contains "arm" then
039   log "ARM MACです"
040else
041   log "インテルMacです"
042end if
043
044
045#アプリケーションはバンドルで対応を調べる
046set ocidBundle to refMe's NSBundle's mainBundle()
047set ocidArchArray to (ocidBundle's executableArchitectures())
048set boolContainARM to ocidArchArray's containsObject:(refMe's NSBundleExecutableArchitectureARM64 as integer)
049if boolContainARM is true then
050   log "ARM MACで使えます"
051end if
052set boolContainIntel to ocidArchArray's containsObject:(refMe's NSBundleExecutableArchitectureX86_64 as integer)
053if boolContainIntel is true then
054   log "インテルMacで使えます"
055end if
AppleScriptで生成しました

| | コメント (0)

バンドルIDからアプリケーションのバージョンの取得(汎用・iOSアプリ対応)

アプリケーションのバージョン取得.scpt

AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#com.cocolog-nifty.quicktimer.icefloe
004# バンドルIDからアプリケーションのバージョンを取得する
005# アプリケーションを起動させない
006# iOSアプリも対応させたいと思ったらこうなった
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008use AppleScript version "2.8"
009use framework "Foundation"
010use framework "AppKit"
011use scripting additions
012property refMe : a reference to current application
013
014#バンドルID
015set strBundleID to ("com.xrite.pantone.pantoneconnect") as text
016#想定されるパス
017set strPresAppPath to ("~/Applications/Pantone Connect.app") as text
018
019##############
020#アプリケーションのパス取得
021set refResponse to doChkAppPath(strBundleID, strPresAppPath)
022if refResponse is false then
023  return "アプリケーションが見つかりませんでした"
024else
025  set {strAppPath, ocidAppPathURL} to refResponse
026end if
027log strAppPath
028
029##############
030#バージョン取得
031set refResponse to doGetAppVerSion(ocidAppPathURL)
032if refResponse is false then
033  return "アプリケーションのバージョン不明"
034else
035  set {ocidVersion, ocidVersionString} to refResponse
036end if
037log ocidVersion as text
038log ocidVersionString as text
039
040
041return
042
043##########################
044#【A】インストール済みチェック
045to doChkAppPath(argBundleID, argPresAppPath)
046  set appFileManager to refMe's NSFileManager's defaultManager()
047  set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(argBundleID))
048  if ocidAppBundle = (missing value) then
049    #バンドルで見つからない場合 NSWorkspace
050    set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
051    set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(argBundleID))
052    if ocidAppPathURL = (missing value) then
053      #AppKitで見つからない場合 Finder
054      try
055        tell application "Finder"
056          set aliasAppApth to (application file id argBundleID) as alias
057          set strAppPath to (POSIX path of aliasAppApth) as text
058        end tell
059        set ocidAppPathStr to (refMe's NSString's stringWithString:(strAppPath))
060        set ocidAppPath to ocidAppPathStr's stringByStandardizingPath()
061        set ocidAppPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAppPath) isDirectory:true)
062      on error
063        set ocidPresAppPathStr to (refMe's NSString's stringWithString:(argPresAppPath))
064        set ocidPresAppPath to ocidPresAppPathStr's stringByStandardizingPath()
065        set ocidPresAppPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPresAppPath) isDirectory:true)
066        set boolDirExists to appFileManager's fileExistsAtPath:(ocidPresAppPath) isDirectory:(missing value)
067        if boolDirExists is true then
068          set ocidAppPath to ocidPresAppPath
069          set ocidAppPathURL to ocidPresAppPathURL
070        else if boolDirExists is false then
071          log "アプケーションを発見できません"
072          return false
073        end if
074      end try
075    end if
076  else
077    #バンドルで見つかる場合
078    set ocidAppPathURL to ocidAppBundle's bundleURL()
079  end if
080  ##########################
081  #本当にあるか?チェック
082  set ocidAppPath to ocidAppPathURL's |path|()
083  set boolDirExists to appFileManager's fileExistsAtPath:(ocidAppPath) isDirectory:(missing value)
084  if boolDirExists is true then
085    log "インストール済み"
086  else if boolDirExists is false then
087    #見つからない Finderで再チェック
088    tell application "Finder"
089      set aliasAppApth to (application file id argBundleID) as alias
090      set strAppPath to (POSIX path of aliasAppApth) as text
091    end tell
092    set ocidAppPathStr to (refMe's NSString's stringWithString:(strAppPath))
093    set ocidAppPath to ocidAppPathStr's stringByStandardizingPath()
094    set ocidAppPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAppPath) isDirectory:true)
095    set strAppPath to ocidAppPathURL's |path|() as text
096    set boolDirExists to appFileManager's fileExistsAtPath:(ocidAppPath) isDirectory:(missing value)
097    #Finderで見つかった
098    if boolDirExists is true then
099      set listResponse to {strAppPath, ocidAppPathURL} as list
100      return listResponse
101    else if boolDirExists is false then
102      #想定パスも探す
103      set ocidPresAppPathStr to (refMe's NSString's stringWithString:(argPresAppPath))
104      set ocidPresAppPath to ocidPresAppPathStr's stringByStandardizingPath()
105      set ocidPresAppPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPresAppPath) isDirectory:true)
106      set boolDirExists to appFileManager's fileExistsAtPath:(ocidPresAppPath) isDirectory:(missing value)
107      if boolDirExists is true then
108        set strAppPath to ocidPresAppPathURL's |path|() as text
109        set listResponse to {strAppPath, ocidAppPathURL} as list
110        return listResponse
111      else if boolDirExists is false then
112        log "アプケーションを発見できません"
113        return false
114      end if
115    end if
116  end if
117  set strAppPath to ocidAppPathURL's |path|() as text
118  set listResponse to {strAppPath, ocidAppPathURL} as list
119  return listResponse
120end doChkAppPath
121
122
123##########################
124#【B】アプリケーションのバージョンの取得
125to doGetAppVerSion(argAppPathURL)
126  set ocidBundle to refMe's NSBundle's bundleWithURL:(argAppPathURL)
127  set ocidInfoDict to ocidBundle's infoDictionary()
128  set ocidVersionString to (ocidInfoDict's valueForKey:("CFBundleShortVersionString"))
129  set ocidVersion to (ocidInfoDict's valueForKey:("CFBundleVersion"))
130  return {ocidVersion, ocidVersionString}
131end doGetAppVerSion
AppleScriptで生成しました

|

[Plist]Acrobat ReaderのSCA(single client app) Unified App版判定(その2 AppleScript:ASOC)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004Plistの内容をBundleを使って取得する
005com.cocolog-nifty.quicktimer.icefloe *)
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use framework "UniformTypeIdentifiers"
011use scripting additions
012
013property refMe : a reference to current application
014
015
016set strBundleID to ("com.adobe.Reader") as text
017
018##########################
019#バンドルの取得
020set ocidAppBundle to refMe's NSBundle's bundleWithIdentifier:(strBundleID)
021#バンドルIDでNSbundleが取得出来なかった場合
022if ocidAppBundle = (missing value) then
023  #NSWorkspaceでアプリケーションのパスの取得を試みる
024  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
025  set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID))
026  # NSWorkspaceでアプリケーションのパスが取得出来なかった場合
027  if ocidAppPathURL = (missing value) then
028    try
029      #Finderでアプリケーションのパスの取得を試みる
030      tell application "Finder"
031        set aliasAppApth to (application file id strBundleID) as alias
032        set strAppPath to (POSIX path of aliasAppApth) as text
033      end tell
034      set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
035      set strAppPath to strAppPathStr's stringByStandardizingPath()
036      set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:(true)
037    on error
038      return "対象のアプリケーションがバンドルIDから見つけられませんでした"
039    end try
040  end if
041  #アプリケーションのパスからNSBundleを定義する
042  set ocidAppBundle to refMe's NSBundle's alloc()'s initWithURL:(ocidAppPathURL)
043end if
044
045##########################
046#SCA判定
047set ocidIsSCA to (ocidAppBundle's objectForInfoDictionaryKey:("AcroSCA"))
048if ocidIsSCA = (missing value) then
049  log "SCA設定の無い古いバージョンを利用しています"
050else if (ocidIsSCA as boolean) = true then
051  log "SCA(single client app) Unified App版です"
052else if (ocidIsSCA as boolean) = false then
053  log "従来版です"
054end if
055
056return
AppleScriptで生成しました

|

バンドルIDからアプリケーションの実行バイナリーへのパスを調べる


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

バンドルIDからアプリケーションの実行バイナリーへのパスを調べる .scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004アプリケーションのバンドルIDから
005アプケーションの実行バイナリのパスを取得する
006puppeteer等で使う
007
008com.cocolog-nifty.quicktimer.icefloe *)
009----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
010use AppleScript version "2.8"
011use framework "Foundation"
012use framework "AppKit"
013use scripting additions
014
015property refMe : a reference to current application
016
017
018###アプリケーションのバンドルID
019set strBundleID to "com.google.Chrome"
020
021
022
023set ocidBinFilePathURL to doGetBinParhURL(strBundleID)
024set strBinFilePath to ocidBinFilePathURL's |path|() as text
025
026###########################
027#バンドルIDの実行バイナリーのパスの取得
028to doGetBinParhURL(argBundleID)
029   set appFileManager to refMe's NSFileManager's defaultManager()
030   set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
031   #NSバンドルを取得して
032   set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(argBundleID))
033   if ocidAppBundle ≠ (missing value) then
034      #NSBundleが取得できればそのまま処理継続
035      set ocidAppPathURL to ocidAppBundle's bundleURL()
036      #NSBundleでアプリケーションが取得できない場合は
037   else if ocidAppBundle = (missing value) then
038      #NSワークスペースでアプリケーションのURLを取得する
039      set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(argBundleID))
040   end if
041   #この時点でアプリケーションを取得できない場合は
042   if ocidAppPathURL = (missing value) then
043      tell application "Finder"
044         try
045            #Fidenerで取得を試みる
046            set aliasAppApth to (application file id strBundleID) as alias
047         on error
048            #Finderでも見つけられない=ディスク上にあっても未起動の場合とか?
049            log "アプリケーションが見つかりませんでした"
050            #ここで諦めてエラーとしてmissing valueを戻す
051            return (missing value)
052         end try
053      end tell
054      #アプケーションを取得できたなら=FinderNSURLにしておく
055      set strAppPath to POSIX path of aliasAppApth as text
056      set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
057      set strAppPath to strAppPathStr's stringByStandardizingPath()
058      set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(true)
059   end if
060   #この時点でアプリケーション取得できない場合はエラー
061   ########################
062   #Plistを調べてバイナリー名を取得
063   set ocidAppBundle to (refMe's NSBundle's bundleWithURL:(ocidAppPathURL))
064   if ocidAppBundle = (missing value) then
065      #アプリケーションは見つかったが、どこのあるか?不明の場合
066      #URLは取得できているのでPLISTから調べる
067      set strSetPlitSubPath to ("Contents/Info.plist") as text
068      set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:(strSetPlitSubPath) isDirectory:(false)
069      set listResponse to refMe's NSDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL) |error|:(reference)
070      set ocidPlistDict to (item 1 of listResponse)
071      set ocidBinName to ocidPlistDict's valueForKey:("CFBundleExecutable")
072      set ocidBinDirPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/MacOS") isDirectory:(true)
073      set ocidBinFilePathURL to ocidBinDirPathURL's URLByAppendingPathComponent:(ocidBinName) isDirectory:(false)
074      return ocidBinFilePathURL
075   end if
076   #テキスト形式でパスを戻すか
077   #      set strExecutableURL to ocidAppBundle's executableURL()'s |path|() as text
078   #      return strExecutableURL
079   #NSURLを戻すか
080   return ocidAppBundle's executableURL()
081   #エイリアスを戻すか
082   #      set aliasFilePath to (ocidAppBundle's executableURL()'s absoluteURL()) as alias
083   #      return aliasFilePath
084   
085end doGetBinParhURL
AppleScriptで生成しました

AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use scripting additions
011
012property refMe : a reference to current application
013
014
015###アプリケーションのバンドルID
016set strBundleID to "com.google.Chrome"
017
018
019
020set ocidBinFilePathURL to doGetBinParhURL(strBundleID)
021set strBinFilePath to ocidBinFilePathURL's |path|() as text
022
023###########################
024#バンドルIDの実行バイナリーのパスの取得
025to doGetBinParhURL(argBundleID)
026  set appFileManager to refMe's NSFileManager's defaultManager()
027  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
028  set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(argBundleID))
029  if ocidAppBundle ≠ (missing value) then
030    set ocidAppPathURL to ocidAppBundle's bundleURL()
031  else if ocidAppBundle = (missing value) then
032    set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID))
033  end if
034  if ocidAppPathURL = (missing value) then
035    tell application "Finder"
036      try
037        set aliasAppApth to (application file id strBundleID) as alias
038      on error
039        log "アプリケーションが見つかりませんでした"
040        return (missing value)
041      end try
042    end tell
043    set strAppPath to POSIX path of aliasAppApth as text
044    set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
045    set strAppPath to strAppPathStr's stringByStandardizingPath()
046    set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(true)
047  end if
048  ########################
049  #Plistを調べてバイナリー名を取得
050  set strSetPlitSubPath to ("Contents/Info.plist") as text
051  set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:(strSetPlitSubPath) isDirectory:(false)
052  set listResponse to refMe's NSDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL) |error| :(reference)
053  set ocidPlistDict to (item 1 of listResponse)
054  set ocidBinName to ocidPlistDict's valueForKey:("CFBundleExecutable")
055  set ocidBinDirPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/MacOS") isDirectory:(true)
056  set ocidBinFilePathURL to ocidBinDirPathURL's URLByAppendingPathComponent:(ocidBinName) isDirectory:(false)
057  return ocidBinFilePathURL
058end doGetBinParhURL
AppleScriptで生成しました

|

バンドルIDからアプリケーションのバーション(Launch Services Register との相違のチェックも入れる)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#com.cocolog-nifty.quicktimer.icefloe
004# Launch Services Register との相違のチェックも入れる
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "UniformTypeIdentifiers"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013set strBundleID to ("com.microsoft.Excel") as text
014
015##########################
016#インストール済みのバージョンチェック
017# Launch Services Registerの値
018set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(strBundleID))
019if ocidAppBundle = (missing value) then
020  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
021  set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID))
022  if ocidAppPathURL = (missing value) then
023    try
024      tell application "Finder"
025        set aliasAppApth to (application file id strBundleID) as alias
026        set strAppPath to (POSIX path of aliasAppApth) as text
027      end tell
028      set strAppPathStr to (refMe's NSString's stringWithString:(strAppPath))
029      set strAppPath to strAppPathStr's stringByStandardizingPath()
030      set ocidAppPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:true)
031    on error
032      set ocidVersionIr to "未インストール" as text
033    end try
034  else
035    set ocidAppBundle to (refMe's NSBundle's bundleWithPath:(ocidAppPathURL's |path|))
036    # set ocidVersionIr to (ocidAppBundle's objectForInfoDictionaryKey:("CFBundleShortVersionString"))
037    set ocidVersionIr to (ocidAppBundle's objectForInfoDictionaryKey:("CFBundleVersion"))
038  end if
039else
040  set ocidAppPathURL to ocidAppBundle's bundleURL()
041  # set ocidVersionIr to (ocidAppBundle's objectForInfoDictionaryKey:("CFBundleShortVersionString"))
042  set ocidVersionIr to (ocidAppBundle's objectForInfoDictionaryKey:("CFBundleVersion"))
043end if
044if ocidVersionIr = (missing value) then
045  try
046    tell application "Finder"
047      set aliasAppApth to (application file id strBundleID) as alias
048      set strAppPath to (POSIX path of aliasAppApth) as text
049    end tell
050    set strAppPathStr to (refMe's NSString's stringWithString:(strAppPath))
051    set strAppPath to strAppPathStr's stringByStandardizingPath()
052    set ocidAppPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:true)
053  on error
054    set ocidVersionIr to "未インストール" as text
055  end try
056end if
057##########################
058#インストール済みのバージョンチェック
059# Plistの値を直接参照
060if ocidVersionIr is "未インストール" then
061  set strVersion to ocidVersionIr as text
062else
063  set ocidInfoFilePathURL to (ocidAppPathURL's URLByAppendingPathComponent:("/Contents/Info.plist") isDirectory:(false))
064  set listResponse to (refMe's NSDictionary's alloc()'s initWithContentsOfURL:(ocidInfoFilePathURL) |error| :(reference))
065  set ocidInfoDict to (item 1 of listResponse)
066  # set ocidVersion to (ocidInfoDict's valueForKey:("CFBundleShortVersionString"))
067  set ocidVersion to (ocidInfoDict's valueForKey:("CFBundleVersion"))
068  if (ocidVersion as text) ≠ (ocidVersionIr as text) then
069    set strVersion to ((ocidVersion as text) & "※") as text
070    log "※:Launch Services Registerの値と相違がありました"
071  else
072    set strVersion to ocidVersion as text
073  end if
074end if
075
076
077return strVersion
AppleScriptで生成しました

|

バンドルIDからアプリケーションのバーション

Launch Services Register の内容によって相違が出る場合があるので非推奨
AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#com.cocolog-nifty.quicktimer.icefloe
004#
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "UniformTypeIdentifiers"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013set strBundleID to ("com.skype.skype") as text
014
015#############
016#インストール済みのバージョンチェック
017set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(strBundleID))
018if ocidAppBundle = (missing value) then
019  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
020  set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID))
021  if ocidAppPathURL = (missing value) then
022    try
023      tell application "Finder"
024        set aliasAppApth to (application file id strBundleID) as alias
025        set strAppPath to (POSIX path of aliasAppApth) as text
026      end tell
027      set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
028      set strAppPath to strAppPathStr's stringByStandardizingPath()
029      set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:true
030    on error
031      set strVersion to "未インストール" as text
032    end try
033  else
034    set ocidAppBundle to (refMe's NSBundle's bundleWithPath:(ocidAppPathURL's |path|))
035    set ocidVersion to (ocidAppBundle's objectForInfoDictionaryKey:("CFBundleVersion"))
036  end if
037else
038  set ocidAppPathURL to ocidAppBundle's bundleURL()
039  set ocidVersion to (ocidAppBundle's objectForInfoDictionaryKey:("CFBundleVersion"))
040end if
041if ocidVersion = (missing value) then
042  try
043    tell application "Finder"
044      set aliasAppApth to (application file id strBundleID) as alias
045      set strAppPath to (POSIX path of aliasAppApth) as text
046    end tell
047    set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
048    set strAppPath to strAppPathStr's stringByStandardizingPath()
049    set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:true
050  on error
051    set strVersion to "未インストール" as text
052  end try
053end if
054set strVersion to ocidVersion as text
055set strAppPath to ocidAppPathURL's |path| as text
056log strVersion
057log strAppPath
AppleScriptで生成しました

|

バンドルIDからアプリケーションのパス


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

########################
## クリップボードの中身取り出し
########################
###初期化
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPastBoardTypeArray to ocidPasteboard's types
###テキストがあれば
set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
if boolContain = true then
  ###値を格納する
  tell application "Finder"
    set strReadString to (the clipboard as text) as text
  end tell
  ###Finderでエラーしたら
else
  set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
  if boolContain = true then
    set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
    set strReadString to ocidReadString as text
  else
log "テキストなし"
    set strReadString to "1" as text
  end if
end if
##############################
#####ダイアログ
##############################
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
activate
  end tell
else
  tell current application
activate
  end tell
end if
set strMes to ("com.domain.app形式のバンドルIDを入力") as text
set aliasIconPath to POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns" as alias
try
  set recordResult to (display dialog strMes with title "入力してください" default answer strReadString buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 10 without hidden answer) as record
on error
log "エラーしました"
return
end try

if "OK" is equal to (button returned of recordResult) then
  set strReturnedText to (text returned of recordResult) as text
else if (gave up of recordResult) is true then
return "時間切れです"
else
return "キャンセル"
end if
###NSStringに格納
set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
###タブと改行を除去しておく
set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
ocidTextM's appendString:(ocidResponseText)
##改行除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
##タブ除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
##出力用のNSString
set strBundleID to ocidTextM as text
log strBundleID
set ocidURL to doGetBundleID2AppURL(strBundleID)
set strPath to (ocidURL's |path|) as text
set strPathURL to ocidURL's absoluteString() as text

#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if

###ダイアログ
set recordResult to (display dialog ("戻り値です\r" & strPathURL) with title "戻り値" default answer strPath buttons {"クリップボードにコピー", "キャンセル", "OPEN"} default button "OPEN" cancel button "キャンセル" giving up after 30 with icon aliasIconPath without hidden answer)
###クリップボードコピー
set strText to text returned of recordResult as text
if button returned of recordResult is "クリップボードにコピー" then
  ####ペーストボード宣言
  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
  set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
else if button returned of recordResult is "OPEN" then
  tell application "Finder"
    set aliasFilePath to POSIX file strText as alias
open aliasFilePath
  end tell
end if


###################################
### バンドルIDからアプリケーションURL
###################################
to doGetBundleID2AppURL(argBundleID)
  set strBundleID to argBundleID as text
  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
  ##バンドルIDからアプリケーションのURLを取得
  set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(strBundleID))
  if ocidAppBundle ≠ (missing value) then
    set ocidAppPathURL to ocidAppBundle's bundleURL()
  else if ocidAppBundle = (missing value) then
    set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(argBundleID))
  end if
  ##予備(アプリケーションのURL)
  if ocidAppPathURL = (missing value) then
    tell application "Finder"
      try
        set aliasAppApth to (application file id strBundleID) as alias
        set strAppPath to (POSIX path of aliasAppApth) as text
        set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
        set strAppPath to strAppPathStr's stringByStandardizingPath()
        set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:true
      on error
return "アプリケーションが見つかりませんでした"
      end try
    end tell
  end if
return ocidAppPathURL
end doGetBundleID2AppURL

|

[localizedStringForKey]ローカライズされたアプリケーション名の取得

サンプルは『Clock.app』-->『時計』

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

###パス
set strFilePath to "/System/Applications/Clock.app" as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
###バンドル
set ocidAppBundle to refMe's NSBundle's bundleWithURL:(ocidFilePathURL)
###取得
set ocidBundleDisplayNameLocalizable to ocidAppBundle's localizedStringForKey:("CFBundleDisplayName") value:("") table:("InfoPlist")
log ocidBundleDisplayNameLocalizable as text
-->(*時計*)


|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat 2024 Acrobat AddOn Acrobat Annotation Acrobat AppleScript Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat GentechAI Acrobat JS Acrobat JS Word Search Acrobat Maintenance Acrobat Manifest Acrobat Menu Acrobat Merge Acrobat Open Acrobat PDFPage Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat Print Acrobat Python Acrobat Reader Acrobat Reader Localized Acrobat Reference Acrobat Registered Products Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat URL List Mac Acrobat URL List Windows Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin ConfigCode Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin NetWork Admin Permission Admin Pkg Admin Power Management Admin Printer Admin Printer Basic Admin Printer Custompapers Admin SetUp Admin SMB Admin softwareupdate Admin Support Admin System Information Admin TCC Admin Tools Admin Umask Admin Users Admin Volumes Admin XProtect Adobe Adobe AUSST Adobe Bridge Adobe Documents Adobe FDKO Adobe Fonts Adobe Reference Adobe RemoteUpdateManager Adobe Sap Code AppKit Apple AppleScript AppleScript Duplicate AppleScript entire contents AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Video Applications AppStore Archive Archive Keka Attributes Automator BackUp Barcode Barcode Decode Barcode QR Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome Chromedriver CIImage CityCode CloudStorage Color Color NSColor Color NSColorList com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Desktop Position Device Diff Disk do shell script Dock Dock Launchpad DropBox Droplet eMail Encode % Encode Decode Encode HTML Entity Encode UTF8 Error EXIFData exiftool ffmpeg File File Name Finder Finder Window Firefox Folder FolderAction Font List FontCollections Fonts Fonts Asset_Font Fonts ATS Fonts Emoji Fonts Maintenance Fonts Morisawa Fonts Python Fonts Variable Foxit GIF github Guide HTML Icon Icon Assets.car Illustrator Image Events ImageOptim Input Dictionary iPhone iWork Javascript Jedit Ω Json Label Language Link locationd lsappinfo m3u8 Mail Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Microsoft Fonts Microsoft Office Microsoft Office Link Microsoft OneDrive Microsoft Teams Mouse Music Node Notes NSArray NSArray Sort NSAttributedString NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSGraphicsContext Crop NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMetadataItem NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSet NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSUbiquitous NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth PDF PDF Image2PDF PDF MakePDF PDF nUP PDF Pymupdf PDF Pypdf PDFContext PDFDisplayBox PDFImageRep PDFKit PDFKit Annotation PDFKit AnnotationWidget PDFKit DocumentPermissions PDFKit OCR PDFKit Outline PDFKit Start PDFPage PDFPage Rotation PDFView perl Photoshop PlistBuddy pluginkit plutil postalcode PostScript PowerShell prefPane Preview Python Python eyed3 Python pip QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver Script Editor Script Menu SF Symbols character id SF Symbols Entity Shortcuts Shortcuts Events sips Skype Slack Sound Spotlight sqlite StandardAdditions StationSearch Subtitles LRC Subtitles SRT Subtitles VTT Swift swiftDialog System Events System Settings TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom Weather webarchive webp Wifi Windows XML XML EPUB XML HTML XML LSSharedFileList XML LSSharedFileList sfl2 XML LSSharedFileList sfl3 XML objectsForXQuery XML OPML XML Plist XML Plist System Events XML RSS XML savedSearch XML SVG XML TTML XML webloc XML xmllint XML XMP YouTube Zero Padding zoom