AppleScript to focus on a window in Preview app - window

Im trying to do some manipulation with preview using Automator. I have several windows open in preview, two of which are of interest. One is named "Markup Badges.png" one is called "Screenshot.png"
I want to set the focus to "Screenshot.png", copy the image and close the window, then I want to close the "Markup Badges.png" window.
I am having a lot of trouble getting this to work.
As part of my experimenting I have created two scripts trying to get the windows in focus, so I can then perform additional actions on them
Script 1:
on run {input, parameters}
tell application "Preview"
set visible of every window whose visible is true to false
end tel
tell application "Preview"
try
set theWindow to 1st window whose name begins with "Screenshot.png"
set index of theWindow to 1
activate
end try
end tell
-- stuff
delay 1
tell application "Preview"
set visible of every window whose visible is false to true
end tell
return input
end run
Script 2:
on run {input, parameters}
tell application "Preview"
set visible of every window whose visible is true to false
end tell
tell application "Preview"
try
set theWindow to 1st window whose name begins with "Markup Badges.png"
set index of theWindow to 1
activate
end try
end tell
-- stuff
delay 1
tell application "Preview"
set visible of every window whose visible is false to true
end tell
return input
end run
If I run Script 1 on its own, it does what I expect, Screenshot.png window is shown and has focus.
If I run Script 2 on its own, it does what I expect, Markup Badges.png window is shown and has focus.
If I run both scripts (play button in automator, Script 1 runs, followed by script 2) then script 2 does not work as it does on its own. The window is shown, but it does not have focus meaning I cannot send any keypresses to the window.
Any help appreciated

I found changing the code to the below fixed the problem.
on run {input, parameters}
tell application "Preview"
set visible of every window whose visible is true to false
end tell
tell application "Preview"
try
set theWindow to 1st window whose name begins with "Markup Badges.png"
set index of theWindow to 1
activate
tell application "System Events" to tell process "Preview" to perform action "AXRaise" of window 1
end try
end tell
-- stuff
delay 1
tell application "Preview"
set visible of every window whose visible is false to true
end tell
return input
end run

Related

How «Wait until application launch» applescript?

How to write the code correctly?
I run the application Photoshop in the automator
I'm waiting for it to fully load
Then I press 10 times Tab and press Enter.
I've tried that:
enter image description here
Looks like that part doesn't work. Because Tab starts to click before the application is fully loaded. What's wrong? Thanks!
repeat until application launch
delay 0.5 end repeat delay 0.5
Most likely, the OP does not understand the main thing: GUI scripting (in this case, sending 10 tabs, and then Enter, that is, keystroke tab and keystroke return in AppleScript language) only works with the frontmost window. And the launch command launches an application without bringing its window to the front.
The correct approach is 1) use the activate application "Photoshop" command 2) use the make new document command, 3) check if the new window exists, 4) send keystroke commands. In the Automator, the Run AppleScript action should be something like this:
on run {input, parameters}
tell application "Photoshop"
activate
make new document with properties {name:"myNewDocument"}
repeat until window "myNewDocument" exists
delay 0.1
end repeat
end tell
tell application "System Events"
repeat 10 times
delay 0.1
keystroke tab
end repeat
keystroke return
end tell
return input
end run
NOTE: not tested, because PhotoShop.app is not installed on my Mac. I am ready to correct my script, if needed. In general, the question is not quite clear.
I don't know much about Photoshop, but I know that it has a loading screen. I tried the following code in Affinity Photo which is a similar product to Photoshop.
tell application "Photoshop"
launch
set theBool to false
repeat until theBool
tell application "System Events" to ¬
if menu item "Close" of ¬
menu 1 of ¬
menu bar item "File" of ¬
menu bar 1 of ¬
application process "Photoshop" exists then ¬
set theBool to true
delay 0.2
end repeat
end tell
The repeat until theBool checks if the loading screen is over by checking if some menu item exists which isn't available when the loading screen is open. If the "Close" and the "File" don't work in Photoshop, you may choose something else.
This is the answer:
tell application "Your app"
launch
activate
end tell

How to get the title of the current Aquamacs window using applescript

I'm trying to programmatically retrieve the major mode of aquamacs. I had a few ideas: get the menubar items, get the window title and parse it using regexes.
I've tried both, and ran into a problem, both the menubar and window arrays are empty, which makes it impossible to do this:
on test()
try
tell application "System Events"
if application "Aquamacs" exists then
-- display notification "It's aquamacs"
tell application "Aquamacs" to activate
if menu bar item "File" of menu bar 1 exists then
display notification "File exists"
--Fails when the file menu bar item clearly is there
else
display notification "File doesn't exist"
end if
else
display notification "It isn't aquamacs"
end if
end tell
end try
end test
test()
or this:
on getAppTitle(appN)
tell application appN
activate
end tell
tell application "System Events"
# Get the frontmost app's *process* object.
set frontAppProcess to first application process whose frontmost is true
end tell
# Tell the *process* to count its windows and return its front window's name.
tell frontAppProcess
if (count of windows) > 0 then --never runs because count is always zero
set window_name to name of every window
end if
end tell
end getAppTitle
getAppTitle("Aquamacs")
and then looking at the file extension.
I don't understand why there's such inconsistency between the system and AppleScript: it clearly has windows which definitely have titles, yet somehow are out of reach of scripts.
The problem is in your code!
In the first block of code, you have if menu bar item "File" of menu bar 1 exists then inside of a tell application "System Events" block without any designated application or application process to query for that information and why it fails.
In the first block of code, there is more then one way to fix it, and one is to change:
if menu bar item "File" of menu bar 1 exists then
To:
if menu bar item "File" of menu bar 1 of application process "Aquamacs" exists then
In the second block of code, tell frontAppProcess equates to:
tell application process "Aquamacs"
Which is why is fails and it needs to be:
tell application "Aquamacs"
In Script Editor with Aquamacs running, run the following code:
tell application "Aquamacs" to count windows
It will return a window count.

How to set a window's index to last (furthest back) in applescript

I've seen a lot of posts for how to send a window to the front in applescript, but I want to be able to send it to the back. How do I write an applescript that will do this?
Maybe you don't actually need to move any windows. Maybe you can just hide your application so your window isn't showing. Since you don't want your window on the top then it's probably OK to just hide your application. It continues running and does its thing but its window doesn't cover any other windows.
Just change "Safari" to the name of your application.
set myAppName to "Safari"
tell application myAppName to activate
tell application "System Events"
-- wait until your application comes forward and then hide it
repeat
set p to first process whose frontmost is true
if name of p is myAppName then
set visible of p to false -- hide your application
exit repeat
end if
delay 0.2
end repeat
end tell
EDIT: if hiding your app doesn't work then you could just keystroke command-tab which is the application switcher command. Basically your app will come to the front and then the keystroke will make the previously frontmost application come to the front. So your window won't go all the way back but it won't be in the front. Maybe that will work.
set myAppName to "Safari"
tell application myAppName to activate
tell application "System Events"
-- wait until your application comes forward
repeat
set p to first process whose frontmost is true
if name of p is myAppName then exit repeat
delay 0.2
end repeat
-- use the application switcher to bring the previously frontmost application forward
keystroke tab using command down
end tell
Something like set index to 999 doesn't seem to work, but set index to (count windows) does:
tell application "TextEdit"
set index of window 1 to (count windows)
end tell
You might also raise all other windows:
tell application "System Events" to tell process "TextEdit"
repeat with w in windows 2 thru -1
perform action "AXRaise" of w
end repeat
end tell
This will move the front finder window to the back...
tell application "Finder" to set index of front Finder window to (count Finder windows)
I have not used "openFrameWorks" so I am not sure of how it works…
But rather than reinvent the wheel with Applescript.
Can you not set the window level in "openFrameWorks"
In xcode/Objective - c I would use the NSWindow Window Levels constants.
To set a normal window:
[awindow setLevel: NSNormalWindowLevel];
But set a window below other normal windows:
[awindow setLevel: NSNormalWindowLevel - 1000];
This will insure the window is always below any normal applications windows. Even when I click on it or drag it. It stays behind other windows.

Embed external application inside a Cocoa SplitView

I would like to have an application under Mac OS X that enables me to have Sublime Text 2 and a terminal (for showing test result, run grunt tasks and so on) in the same fullscreen window. I couldn't find an application whith this behaviour and I think of reproducing it myself with cocoa split view.
I would like to know if it's possible and, if yes, how can I start implementing it
Thank you
You can't create a new application from 2 other applications. It won't work. However you can use applescript to make it easy for you to position these windows as you want.
As an example I'll use Safari and Terminal as my 2 applications. Open them and place them on the screen as you want them to appear. I opened each window large and positioned them side-by-side. Then I ran this applescript to get their window size and position properties...
tell application "System Events"
tell process "Safari"
set safariSize to size of window 1
set safariPosition to position of window 1
end tell
tell process "Terminal"
set terminalSize to size of window 1
set terminalPosition to position of window 1
end tell
end tell
return {safariSize, safariPosition, terminalSize, terminalPosition}
Then I copy/pasted the result from that script into the "theValues" variable in this script. Now whenever I want I can run this script to recreate those window positions.
set theValues to {{1001, 1025}, {0, 22}, {613, 1024}, {1003, 22}}
tell application "Safari" to activate
tell application "Terminal" to activate
tell application "System Events"
tell process "Safari"
set size of window 1 to item 1 of theValues
set position of window 1 to item 2 of theValues
end tell
tell process "Terminal"
set size of window 1 to item 3 of theValues
set position of window 1 to item 4 of theValues
end tell
end tell
I hope that helps. Good luck.

Applescript - Bring window to foreground

I have an application with several windows opened at the same time.
I'd like to bring a specific window to foreground (I know its title).
At the moment I'm using a combination of keys to achieve this task but I'd like to try something different since I'm experiencing some problems with this approach.
tell application "System Events"
set frontmost of process "appIT" to true
keystroke "1" using command down
delay 0.2
end tell
This is possible by using the "AXRaise" action, except on certain window (applications that use X11 for example).
Try this.
set theTitle to "some title"
tell application "System Events"
tell process "appIT"
set frontmost to true
perform action "AXRaise" of (windows whose title is theTitle)
end tell
end tell
If your application is scriptable and allows setting the index of a window, you can do the following (based on an answer in How do I make a Safari window active using AppleScript (elegantly)?)
to raiseWindow of theApplicationName for theName
tell the application named theApplicationName
activate
set theWindow to the first item of ¬
(get the windows whose name is theName)
if index of theWindow is not 1 then
set index to 1
set visible to false
set visible to true
end if
end tell
end raiseWindow
The toggling of the visibility is necessary to deal with some weirdness that occurs with switching applications. If you don't toggle the visibility, the window won't be the first when you switch away from and back to the application. Unfortunately, this toggling shrinks the window to the dock then restores it, a very dramatic UI disruption.
Here's another way I've found to deal with the weirdness:
to raiseWindow2 of theApplicationName for theName
tell the application named theApplicationName
activate
set theWindow to the first item of ¬
(get the windows whose name is theName)
if the index of theWindow is not 1 then
set the index of theWindow to 2
tell application "System Events" to ¬
tell application process theApplicationName to ¬
keystroke "`" using command down
end if
end tell
end raiseWindow2
I don't think System Events can change the front window of a process. Of course you can close the front window until the window you want is on top. That's not really a solution though as you probably don't want to close windows. Really though the only way you could achieve this is if the application itself is apple-scriptable and allows you to do this.

Resources