AppleScriptで他のアプリケーションプロセスの情報を取得するには、OS標準装備のSystem Eventsに対して、
tell application "System Events" set aProp to properties of process "Safari" end tell --> {has scripting terminology:true, bundle identifier:"com.apple.Safari", file:alias "Macintosh HD:Applications:Safari.app:" of application "System Events", creator type:"sfri", subrole:missing value, entire contents:{}, selected:missing value, application file:alias "Cherry:Applications:Safari.app:" of application "System Events", orientation:missing value, role:"AXApplication", accepts high level events:true, file type:"APPL", value:missing value, position:missing value, id:909534, displayed name:"Safari", name:"Safari", class:application process, background only:false, frontmost:false, size:missing value, visible:true, Classic:false, role description:"application", maximum value:missing value, architecture:"x86_64", partition space used:0, short name:"Safari", focused:missing value, minimum value:missing value, help:missing value, title:"Safari", accepts remote events:false, total partition size:0, description:"application", accessibility description:missing value, enabled:missing value, unix id:3386}
などと操作することになります。ただし、System Eventsがつねに使えるわけではありません。
Mac App Storeに出すアプリケーションの中だと、些細な用途に他のアプリケーションを呼び出そうとしても、よほどの理由がないかぎり通りません(リジェクトされます)。Dark Mode/Light Modeの検出に安直にSystem Eventsを使おうとしてリジェクトされた経験があります。
そこで、他のサービス(shell commandとか、Cocoa Frameworkとか)を経由して機能を呼び出すことになります。まったく同じことができるわけではありませんが、1つの目的に対して複数の方法を用意しておくのはセオリーです。OS側でバグを作られた場合の回避策とか(正しくないOSバージョンを返してくるmacOSがありました)、目的に応じて利用に制限がかかる用途(まさにMac App Storeがそれです)があるためです。
AppleScript名:NSRunningApplicationでアプリケーションプロセス情報を取得 |
— Created 2017-09-17 15:27:23 +0900 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set aBundleID to "com.apple.Safari" set appArray to current application’s NSRunningApplication’s runningApplicationsWithBundleIdentifier:aBundleID if appArray’s |count|() = 0 then its return set appItem to appArray’s objectAtIndex:0 set iconRes to (appItem’s icon()) (* –> (NSImage) <NSImage 0x618001478f40 Size={32, 32} Reps=( "<NSIconRefImageRep:0x61800029b300 iconRef=0x8403 size:128×128 pixels:128×128>", "<NSIconRefImageRep:0x61800089f7c0 iconRef=0x8403 size:128×128 pixels:256×256>", "<NSIconRefImageRep:0x6180006989c0 iconRef=0x8403 size:256×256 pixels:256×256>", "<NSIconRefImageRep:0x61800129f040 iconRef=0x8403 size:256×256 pixels:512×512>", "<NSIconRefImageRep:0x618000898dd0 iconRef=0x8403 size:512×512 pixels:512×512>", "<NSIconRefImageRep:0x61800029a270 iconRef=0x8403 size:48×48 pixels:48×48>", "<NSIconRefImageRep:0x618000691490 iconRef=0x8403 size:36×36 pixels:36×36>", "<NSIconRefImageRep:0x618000c8e100 iconRef=0x8403 size:36×36 pixels:72×72>", "<NSIconRefImageRep:0x618000a80460 iconRef=0x8403 size:32×32 pixels:32×32>", "<NSIconRefImageRep:0x6180004899c0 iconRef=0x8403 size:32×32 pixels:64×64>", "<NSIconRefImageRep:0x61800089db50 iconRef=0x8403 size:18×18 pixels:18×18>", "<NSIconRefImageRep:0x618001291530 iconRef=0x8403 size:18×18 pixels:36×36>", "<NSIconRefImageRep:0x618000c8e0b0 iconRef=0x8403 size:16×16 pixels:16×16>", "<NSIconRefImageRep:0x618000880280 iconRef=0x8403 size:16×16 pixels:32×32>", "<NSIconRefImageRep:0x618000e99a50 iconRef=0x8403 size:512×512 pixels:1024×1024>" )> *) set locRes to (appItem’s localizedName()) as string –> "Safari" set bID to (appItem’s bundleIdentifier()) as string –> "com.apple.Safari" set bURL to (appItem’s bundleURL()) as string –> "Cherry:Applications:Safari.app:" set arch to (appItem’s executableArchitecture()) –> 16777223 set exeURL to (appItem’s executableURL()) –> (NSURL) file:///Applications/Safari.app/Contents/MacOS/Safari set launchDate to (appItem’s launchDate()) –> (NSDate) 2017-09-13 01:58:16 +0000 set launchFinish to (appItem’s finishedLaunching) as integer –> 1 set pID to (appItem’s processIdentifier) as integer –> 11877 set oenMenu to (appItem’s ownsMenuBar()) as boolean –> false |
More from my site
(Visited 237 times, 1 visits today)