I’ve been experimenting with the event handler example (from Shane’s book) as a way to ensure that app windows (opened by my parent app) are opened in sequence, so they can be moved and stacked in the correct order.
-
Could someone give me a bit of feedback on whether I’m doing this correctly?
-
Is there a way to make sure that a detected launch notification was the result of my last “open” command and not from another script?
-
Since I want to detect the window resulting from the “open” command to be present (so it can be moved/resized), should I be using “NSWorkspaceSessionDidBecomeActiveNotification” instead of “NSWorkspaceDidActivateApplicationNotification”?
use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit" -- for NSWorkspace
use scripting additions
-- global flag to indicate the app was acivated
global appWasActivatedFlag
on run {input, parameters}
-- Define the files we will open
set theTextFile to (((path to the desktop) as string) & "Test.txt") as alias
-- display dialog theTextFile as string
set thePdfFile to (((path to the desktop) as string) & "Test.pdf") as alias
-- display dialog thePdfFile as string
-- register the appWasActivated handler
watchForAppLaunches()
-- open the text file
tell application "TextEdit"
activate
open theTextFile
end tell
-- wait until TextEdit is activated
display dialog "Waiting for TextEdit Activation event"
repeat until appWasActivatedFlag is true
delay 0.1
end repeat
-- reset appWasActivatedFlag
set appWasActivatedFlag to false
-- open the pdf file
tell application "Preview"
activate
open thePdfFile
end tell
-- deregister the appWasActivated handler
stopWatchingForAppLaunches()
return input
end run
on watchForAppLaunches()
set theNSWorkspace to current application's NSWorkspace's sharedWorkspace()
set theNSNotificationCenter to theNSWorkspace's notificationCenter()
theNSNotificationCenter's addObserver:me selector:"appWasActivated:" |name|:(current application's NSWorkspaceDidActivateApplicationNotification) object:(missing value)
set appWasActivatedFlag to false
set theAppLoaded to false
end watchForAppLaunches
on appWasActivated:theNSNotification
set userInfo to theNSNotification's userInfo()
set theApp to userInfo's valueForKey:(current application's NSWorkspaceApplicationKey)
set theAppName to (theApp's localizedName()) as text
display notification (theAppName & " was launched") sound name "Ping"
set appWasActivatedFlag to true
end appWasActivated:
on stopWatchingForAppLaunches()
set theNSWorkspace to current application's NSWorkspace's sharedWorkspace()
set theNSNotificationCenter to theNSWorkspace's notificationCenter()
theNSNotificationCenter's removeObserver:me |name|:(current application's NSWorkspaceDidActivateApplicationNotification) object:(missing value)
set appWasActivatedFlag to false
set theAppLoaded to false
end stopWatchingForAppLaunches
Model: MacBook Pro (retina)
AppleScript: 2.9
Browser: Firefox 64.0
Operating System: macOS 10.14