Wednesday, February 20, 2019

#1 2019-02-07 03:31:44 pm

GG
Member
Registered: 2019-01-14
Posts: 86

Event Handler to enforce sequencial appearance of app windows

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"?


Applescript:

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

Offline

 

#2 2019-02-08 10:22:25 am

GG
Member
Registered: 2019-01-14
Posts: 86

Re: Event Handler to enforce sequencial appearance of app windows

I think I've stumbled upon why the windows in my scripts are not coming up in the order they were opened. It is not likely that notifications would be the easiest way to solve this.

I tried  using a list of files to open with a single default app and found that the order of the windows on the desktop was the order they were commanded to open (latest on top). However; if the list order is random, such that opening via different apps are interleaved, then the desktop order of stacking in not the order in which they were opened. I'd love to speculate why this is, but it sounds like there is a simple answer.

It seems that I need to sort the array of URLs on default app name, prior to running through the list to open the files.

Now, I just need to figure out a speedy way sort an array of URLs...

I may be able to do this in AppleScriptObjC and add it into the routine that builds a filtered list of files in a directory. As such, I'll post a question as an addition to my other post on the that routine.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)