Unable to activate Atom with Applescript - applescript

I'm trying to just switch to Atom and resize the window with basic Applescript:
tell application "Atom"
activate
set bounds of front window to {0, 495, 1680, 1050}
end tell
If Atom is not open, it launches but then the script throws an error and it doesn't resize the window:
An error of type -10810 has occurred.
If Atom is already running it just throws an error and doesn't even switch to Atom:
"Atom Helper got an error: Application isn’t running."

Oh okay I just noticed that for some reason it automatically changes "Atom" to "Atom Helper" whenever I run the script. So changed it to reference the full path "/Applications/Atom.app instead and now it does exactly what I wanted.
tell application "/Applications/Atom.app"
activate
set bounds of front window to {0, 495, 1680, 1050}
end tell

Related

Where to find the a list of all application name available for tell application apple script?

For example in the tutorial here
tell application "Finder"
Finder is an app in my system.
Terminal as well.
However, if I replace Terminal with iTerm which is also an App named like that in my applications list, it wouldn't work. Even it is shown in some of the answer from SO like following
#!/bin/bash
osascript -e '
tell app "iTerm"
activate
tell the first terminal
launch session "Default Session"
tell the last session
set name to "New Session"
write text "cd /usr/bin; ls"
end tell
end tell
end tell'
it will return error of
52:60: syntax error: Expected class name but found identifier. (-2741)
Or something like
#!/bin/bash
osascript -e '
tell application "Terminal"
do script "date"
activate
end tell'
It works, but when I replace Terminal to iTerm it returns error of 35:41: syntax error: Expected end of line but found “script”. (-2741)
So suspect I am using the wrong app name even it appears to be iTerm is the actual app name, but it seems not, as it caused error above. If I can have a list of the actual app names I can use, it may help me to tackle it
*NOTE: I do have iTerm installed, and that's why I want to refer to it...

Why this bug - applescript , automator & applescript editor

My applescript gave correct output when ran inside automator as service with no input but it gives error message when ran outside automator (safari, preview etc.)
It also gives correct output when ran through applescript editor, no problem running it with safari, preview or any other application.
The reason I was so keen to get it done with automator was that I don't want to install a 3rd party app just for assigning one shortcut to one script.
I debugged this portion which after removing from code allowed automator to run the script. Ran it with try, error in automator & viola, no issues at all.
But I want to know why this code earlier & without try-error function was giving the error messages when ran outside automator application.
Here's the portion of the script with try-error function:
try
set writ to do shell script "/usr/bin/python Users/[username]/Documents/tech_toolkit/windtitle"
tell application writ
if the (count of windows) is not 0 then
set window_title to name of front window
end if
end tell
on error
delay 2
end try
For shell script /windtitle , windtitle is an executable file
#!/usr/bin/env python
from AppKit import NSWorkspace
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
print active_app_name
The error is:
Your python script return the "Automator Runner" application when it run as service , it's
the frontmost in the background but not in the foreground.
To get the frontmost application, use this script:
try
tell application (path to frontmost application as text)
set window_title to name of front window
set writ to its name -- get the name of this application
end tell
on error
return -- quit this script, because this application is not scriptable, or no window
end try

Opening multiple windows with Applescript

I am trying to write an Applescript to open three VLC windows in different screen positions. The script opens three instances of VLC but has them one on top of the other (using the position for window 1). Help with the code appreciated:
do shell script "open -n /Applications/Video/VLC.app"
tell application "System Events"
activate
set bounds of first window of application "VLC" to {13, 36, 790, 519}
end tell
do shell script "open -n /Applications/Video/VLC.app"
tell application "System Events"
activate
set bounds of second window of application "VLC" to {13, 544, 790, 1027}
end tell
do shell script "open -n /Applications/Video/VLC.app"
tell application "System Events"
activate
set bounds of third window of application "VLC" to {13, 1043, 790, 1526}
end tell
#khagler's comment provides the right pointer: the VLC instances must be distinguished by their PIDs (process IDs; called unix id in AppleScript) in the System Events context.
The code below should do what you want, arrived at after much toil and trouble -- par for the [AppleScript obstacle] course. One obstacle was that the VLC instances' main windows do not get created right away.
The comments provide more details.
Note that because user-interface elements are programmatically manipulated, the application running your script must be granted assistive access for security reasons.
Note that I'm starting the instances with do shell script "open -na VLC.app", relying on the location of the app being known to Launch services (should that not work for some reason, revert to your method of specifying the full path).
# Specify the desired window bounds.
# !! In the "System Events" context, windows do not
# !! have `bounds` properties, but separate `position` and
# !! `size` properties.
set WIN_POSITIONS to {{13, 36}, {13, 544}, {13, 1043}}
set WIN_SIZES to {{790, 519}, {790, 519}, {790, 519}}
# Launch the VLC instances.
repeat with i from 1 to count of WIN_POSITIONS
do shell script "open -na VLC.app"
end repeat
# Note:
# Instance-specific manipulation must
# be performed in the "System Events" context, because
# we must distinguish the VLC instances by their
# PIDs (process IDs; called `unix id` in AppleScript).
tell application "System Events"
# Get the PIDs (process IDs) of all VLC instances.
set vlcPids to get the unix id of every process whose name is "VLC"
# Loop over all instance PIDs.
# !! It is imperative to *continue* to use object specifiers
# !! with *filters based on the PID* so as to ensure that the
# !! individual instances are targeted.
# !! Attempting to store references to these instances in
# !! variables fails subtly, as evidenced by the "Events"
# !! tab in AppleScript editor later showing the non-specific
# !! process "VLC" of application "System Events" specifiers.
set winNdx to 1
repeat with vlcPid in vlcPids
# WAIT for each instance to create its main window, wich
# sadly, is not available right away.
# Once created, position it.
set haveWin to false
tell (first process whose unix id is vlcPid)
repeat with i from 1 to 25 # times out after 25 * .2 == 5 secs.
if (count of windows of it) > 0 then
set haveWin to true
tell front window of it
# !! In the "System Events" context, windows do not
# !! have `bounds` properties, but separate `position` and
# !! `size` properties.
set position to item winNdx of WIN_POSITIONS
set size to item winNdx of WIN_SIZES
end tell
exit repeat
end if
delay 0.2 # no window yet; sleep some and try again
end repeat
end tell
if not haveWin then error "VLC instance " & vlcPid & " unexpectedly did not create a window within the timeout period."
set winNdx to winNdx + 1
end repeat
end tell
How to make this work with Finder:
Targeting Finder changes the approach for two reasons:
there's only one Finder instance.
you cannot open multiple windows with open -na Finder.app; thankfully, this answer shows how to do it (see the comments there for quirks).
Note that the following blindly opens additional Finder windows.
set WIN_POSITIONS to {{13, 36}, {13, 544}, {13, 1043}}
set WIN_SIZES to {{790, 519}, {790, 519}, {790, 519}}
# Sample target locations for the Finder windows.
# Note the use of the "System Events" context to faciliate use of
# POSIX-style *input* paths; note, however, that the paths are
# *stored* as HFS paths so that Finder accepts them.
tell application "System Events"
set WIN_TARGETS to {¬
path of desktop folder, ¬
path of folder "~/Downloads", ¬
path of folder "/Library/Audio"}
end tell
set winCount to count of WIN_POSITIONS
# Launch the Finder windows.
tell application "Finder"
# Create the windows in reverse orders.
repeat with i from winCount to 1 by -1
set newWin to make new Finder window
set target of newWin to item i of WIN_TARGETS
end repeat
end tell
tell application "System Events"
set i to 1
repeat with i from 1 to winCount
tell window i of application process "Finder"
# !! In the "System Events" context, windows do not
# !! have `bounds` properties, but separate `position` and
# !! `size` properties.
set position to item i of WIN_POSITIONS
set size to item i of WIN_SIZES
end tell
end repeat
end tell
After scouring the internet and much trial & error I have found this works well providing you allow the AppleScript Editor (or automator or your own app, whichever you are using) to control your computer under System Preferences > Security & Privacy > Privacy > Accessibility (OS X 10.9 Mavericks). The first time you run the script/app you'll be prompted to change the setting. After granting access, the second time you run the script/app the windows will be re-positioned.
do shell script "open -n /Applications/VLC.app /path/to/first/file" --edit path
do shell script "open -n /Applications/VLC.app /path/to/second/file" --edit path
delay 3 --wait 3s for VLC to open files, increase if necessary
tell application "System Events"
set pidList to the unix id of (every process whose name contains "VLC")
tell (first process whose unix id is item 1 of pidList)
set the position of the front window to {0, 22} --edit {x,y}
end tell
tell (first process whose unix id is item 2 of pidList)
set the position of the front window to {640, 22} --edit {x,y}
end tell
end tell
It's not as clever as mklement0's answer but, as someone who is new to AppleScript, I could understand what's going on.

Run a script in an existing Terminal window when the current line already contains some text

If the current line in the frontmost Terminal tab contained the text aa, this would run the command aauptime:
set cmd to "uptime"
tell application "Terminal"
try
do script cmd in window 1
on error
do script cmd
end try
activate
end tell
You could obviously add something like tell app "System Events" to keystroke "ku" using control down before do script, but does anyone know any better solutions?
You know you can call a shell script without targeting the terminal, right?

An applescript stopped working after upgrade from snow leopard to lion

This script worked every time with Snow Leopard.
tell application "Terminal"
activate
do script "cd web_sites/project" in front window # this line highlighted on error
do script "mate ." in front window
do script "rvm 1.8.7" in front window
do script "script/server" in front window
delay 4
do shell script "open -a Firefox http://localhost:3000"
end tell
With Lion I keep getting this error:
error "Terminal got an error: Can’t get window 1." number -1728 from window 1
Thanks.
I'm running Lion. I can execute the following with no errors. I do not get an error in Applescript in any of the following situations 1) the application is not running, 2) the app is running and a window is open, and 3) the app is running and the directory path is not valid. In case 3 the Terminal shows an error but applescript does not.
tell application "Terminal"
activate
do script "cd Development/Images" in front window -- this line highlighted on error
do script "ls -al" in front window
end tell
So your problem is something not related to this actual code.
It seems the problem is not the code but the speed at which the operating system is functioning. As i mentioned before, the code executed just fine in Snow Leopard. To compensate, after the upgrade to Lion, if i add another delay to give Terminal time to finish activating, and increase the delay before opening Firefox, everything works. My laptop is a MacBook Pro with 2.7 GHz Intel Core i7 processor.
tell application "Terminal"
activate
delay 1
do script "cd web_sites/project" in front window # this line highlighted on error
do script "mate ." in front window
do script "rvm 1.8.7" in front window
do script "script/server" in front window
delay 5
do shell script "open -a Firefox http://localhost:3000"
end tell
If you are just trying to get a script to run once, simply retrying may help. Execution speed is also a function of other demands on the system.

Resources