How «Wait until application launch» applescript? - 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

Related

Click on a specific place on a window, click on a specific button and control if the window changes

Actually I have 3 questions about the same problem: controlling a window with applescript.
What should I do if I would press on button "Close Window" of application "Google Chrome"?
Is it possible to check if the window changes? For example, to see if appear a pop-up or something like that...
What about clicking on a specific place into a window? I mean, I know I can use
tell application "System Events"
click at {x,y}
end tell
but this command use the entire screen as reference system, and I want it works only on a specific window. For example, if at "{x,y}" i put "{1,1}", applescript will click on the first item on the menu bar. Is there a way I can say to "System Events" to click at "{1,1}", but on the window "Google Chrome"?
Here are three examples of how to close the front window of Google Chrome using AppleScript:
Note: The following assumes Google Chrome is running with at least one window open when you test each example AppleScript code in Script Editor.
Example one is the most straight forward way:
tell application "Google Chrome" to close front window
Example two directly clicks the close button:
tell application "System Events" to tell ¬
application process "Google Chrome" to ¬
click button 1 of front window
Example three calculates the center of the close button and clicks there:
activate application "Google Chrome"
delay 0.5
tell application "System Events" to tell ¬
application process "Google Chrome" to tell ¬
front window
set posB1 to (position of button 1)
set szB1 to (size of button 1)
set x to (item 1 of posB1) + (item 1 of szB1) / 2 as integer
set y to (item 2 of posB1) + (item 2 of szB1) / 2 as integer
end tell
tell application "System Events" to click at {x, y}
Note that in the first two examples, the front window of Google Chrome doesn't even need to be the frontmost window on the Desktop; however, with the third example it does, otherwise the click at {x, y} will not go to the intended target.
That said, example three really shouldn't be used when there it a straight forward way, as in example one, to get the job done. Example three was just a proof of concept to get the coordinates to click at. This method may be useful in some fringe cases, especially in an app that doesn't directly support AppleScript.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
In applescript GUI scripting you can simply refer to an element by name or index and tell it to click or to perform an action. For instance to click the close button on the first open window in Chrome you could use:
tell application "System Events"
tell process "Google Chrome"
tell window 1
tell button 1
click
end tell
end tell
end tell
end tell
You don't actually need to know its physical position to click one it; you just need to know that the first button in the window is the close button.
System Events always returns the position of any element in screen pixels, so if you want the position of an element in terms of its window, get the position of the element, get the position of the window, and do some addition or subtraction (e.g., if you want to click at {5,5} in a window whose position is {100, 125}, click at {105, 130})
AppleScript isn't really designed to monitor GUI changes, though if you want to be tricky and you know what change you're looking for you can do something like this:
tell application "System Events"
tell process "..."
tell window 1's pop up button 3
repeat until (exists menu 1)
delay 0.2
end repeat
-- menu 1 now exists, so the pop up button is open
end tell
end tell
end tell
...but note that this will hang the script until the menu is opened. A more elegant way to handle that is to write a script application with an idle handler, like so:
on run
-- whatever initialization is needed
end run
on idle
tell application "System Events"
try
tell process "..."
tell window 1's pop up button 3
if exists menu 1 then
-- menu 1 now exists
-- the pop up button is open
-- do what must be done
end if
end tell
end tell
on error errstr
display alert "Something went wrong" message "The script sent this error: " & errstr
end try
end tell
return 0.2
end idle
You can leave that running in the background watching for specific changes in the GUI (the 'try' statement is in case the app you're watching quits, the window closes, or something unexpected happens to the GUI).
If you haven't already, open the System Events scripting definition in Script Editor and look at the Processes Suite. That will show you all the things you can do with GUI scripting.

Using applescript to clear ITerm2 buffer

"Clear buffer" is a menu option under Iterm2's "Edit" menu (command-K) . I'd like to script this to clear Iterm's buffer.
I've tried, based on another site's suggestions,
tell theSession
select
tell application "System Events" to tell process "iTerm2"
click menu item "Clear Buffer" of menu 1 of menu bar item "Edit" of menu
bar 1
end tell
end tell
I've also tried
tell theSession
select
tell application "System Events"
delay 0.1
keystroke "L" using command down
end tell
end tell
Neither seems to do anything. Any ideas?
Tested under macOS 10.13.5 using iTerm2 Build 3.1.7, the default keyboard shortcut for the Clear Buffer command is ⌘K, as shown in the image below.
The following example AppleScript code will activate iTerm, and act on the frontmost window to clear the buffer:
tell application "System Events"
click UI element "iTerm" of list 1 of application process "Dock"
delay 0.25
try
keystroke "k" using command down
end try
end tell
Or use:
tell application "iTerm" to activate
delay 0.25
tell application "System Events"
try
keystroke "k" using command down
end try
end tell
Note that lowercase k is used even though the menu shows an uppercase K. If you have modified the Clear Buffer keyboard shortcut to use ⌘L, then use a lowercase l.

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.

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.

Applescript - System events - Set default location for "open file" popup window

Im using system events to control a program that does not have a applescript library.
I am therefor using system events to control it.
I have gotten the program to open a pop up window for it Open File interface and I would like to get it to default to a certain location. Is this possible.
So Far I have :
tell application "App Name"
activate
end tell
tell application "System Events"
tell process "App Name"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
tell menu item "Import"
tell menu "Import"
click menu item "XML..."
delay 4
end tell
end tell
end tell
end tell
end tell
end tell
end tell
The pop up window defaults to its own last visited location. I would like it to default to a given file path like /Users/userabc/Documents/abcd.XML
Thanks
If you have the "posix path" of a location and the dialog box open, you can do the following. Note that the location can be a folder or a file path. If it's a file path then that file will be selected and you would then just have to "keystroke return" to close the dialog box and open that file. Good luck.
set theLocation to path to home folder
set posixLocation to POSIX path of theLocation
tell application "System Events"
keystroke "g" using {command down, shift down}
delay 0.5
keystroke posixLocation
delay 0.5
keystroke return
end tell
The only problem with this method is that autocorrect starts filling in as apple script types into the text box and screws everything up. Work around is to copy/paste into from applescript.
The keystroke command doesn't work for inserting characters that can't be inserted with the current input source. And it doesn't work at all with some input sources.
You could also set the value of the text field:
tell application "System Events" to tell (process 1 where frontmost is true)
keystroke "g" using {shift down, command down}
tell window 1
tell sheet 1
set value of text field 1 to "/usr/share/dict/connectives"
click button 1
end tell
click button "Open"
end tell
end tell

Resources