How to make applescript not automatically open applications in tell blocks - applescript

I am having issues with applescript, where whenever I open an applescript file (either in AppleScript Editor or in Automator) it will automatically open any programs mentioned in a tell statement/block. Most of these tell blocks are only reachable through conditionals and I would like to find a way to stop applescript's default behavior of opening all mentioned applications.
This is basically what I am doing:
if currentApplication is "Application 1" then
tell application "Application 1"
do stuff
end tell
else if currentApplication is "Application 2" then
tell application "Application 2"
do stuff
end tell
end if
Unfortunately, whenever I open this script it will open both "Application 1" and "Application 2" if they are not open yet, even though it should only get to the tell statements if one of the applications is open. The way I get the current application is not really relevant, even if my 'if' statements were "if false" and I cut out the method that gets the "current" (or closest) application, the applications will still be opened along with the applescript.

If you just run a compiled script, an application will only be launched if AppleScript needs to send it a command, but if you open the script in a script editor, all of the applications referenced in the script will be launched in order to get their custom terminology - see the AppleScript Release Notes.

Related

AppleScript: loop over application windows; bring each to front & trigger action in other app?

Short Version
I want to write some AppleScript which iterates over an application's (non-minimised) windows and for each of them, brings it to the front and triggers something in another app, BTT (which will move the window). The aim is to move all windows of the app to the same place. The moving part is handled (by BTT), I just need help with making this happen to all the app's windows.
Long Version
(This question involves BetterTouchTool but it's not really about that - I think - and I suspect you don't need to know anything about BTT to answer...)
I'm trying to write an AppleScript script which (when I boil it down to the simplest version possible) activates some app, and loops over all its windows, triggering a BetterTouchTool "named trigger" for each window. The job of the named trigger is to move the current window to some location, but the details of that don't matter.
This feels to me like it should be straightforward, but I don't seem to be able to find the right approach. I've tried cargo-culting it but I'm not getting anywhere (other than a suspicion I'll need to involve "System Events")... I'm an experienced programmer but I've never gone deep into AppleScript and find its way of doing things rather weird/confusing.
What I've got working so far is a version which doesn't loop over the windows, so it just hits the "first" one (in some sense of "first", which seems to be "most recently active for this app").
For example, the following works. (I've hard coded it here to VSCode, though in the full thing I'm trying to write, it iterates over a list of app names and the triggers to run for them.)
on run
if application "Visual Studio Code" is running then
tell application "Visual Studio Code" to activate
tell application "BetterTouchTool"
trigger_named "Move window: monitor 4k: core"
end tell
end if
end run
So that activates VSCode, then tells BTT to run a named trigger to move the current window to some position on the screen. The effect is that the most recently used VSCode window gets moved.
OK, not a bad start, but I want to do this for all VSCode windows (or ideally, all non-minimised ones on currently visible desktops).
BTT's AppleScript interface doesn't seem to support targetting particular windows - it just works on whatever app/window is active, so I need to somehow activate/bring to front (?) the window then call the BTT trigger.
I guess I need something like this (ignoring, for now, the problem of omitting minimised windows):
on run
if application "Visual Studio Code" is running then
tell application "Visual Studio Code" to activate
repeat with app_window in windows
bring_window_to_front()
tell application "BetterTouchTool"
trigger_named "Move window: monitor 4k: core"
end tell
end repeat
end if
end run
... where I don't know how to do the bring_window_to_front() part!
(Also not considered here: other desktops; but I can live without that I think.)
Could someone point me in the right direction? All help appreciated!
Summary
I want to:
Given an application identified by a given name...
get all of its windows and loop over them, and for each:
activate/bring that window to the front (provided it's not minimised) and...
tell BetterTouchTool to run the trigger with the given name.
Context
MacOS Mojave 10.14.5
BetterTouchTouch 3.072 (not that I think this makes any difference)
Any scriptable app — I'm assuming "Visual Studio Code" is scriptable (has a scripting dictionary) from what you wrote — should respond to this code:
on run
if application "Visual Studio Code" is running then
tell application "Visual Studio Code" to activate
set wList to every window whose visible is true
repeat with app_window in wList
set index of app_window to 1
delay .5
tell application "BetterTouchTool"
trigger_named "Move window: monitor 4k: core"
end tell
end repeat
end if
end run
The ... whose visible is true part of line 4 excludes hidden or minimized windows.
The set index of ... command in line 6 cycles each window to the front.
index and visible are part of the standard Window properties that all AppleScript developers are supposed to implement, so unless the app designers are complete amateurs they should 'just work.' Look at the scripting dictionary in Script Editor to be sure. Sometimes AppleScript designers add special commands that are useful, or change the names of standard properties (possibly just to be annoying).
I'm not sure if the delay .5 in line 7 is needed. I threw it in to be overly-sure the window was frontmost before the BTT call.
Don't use GUI-scripting or System Events unless the app is non-scriptable. GUI-scripting is extremely useful when it is needed, but if the app has a scripting dictionary using that will be far more efficient and reliable. GUI-scripting is clunky, slow, and subject to unpredictable disruptions. But if you do need to use it, the code is similar.
tell application "System Events"
tell process "Visual Studio Code"
set frontmost to true
set wlist to every window
repeat with aWindow in wlist
tell aWindow
perform action "AXRaise"
delay .5
tell application "BetterTouchTool"
trigger_named "Move window: monitor 4k: core"
end tell
end tell
end repeat
end tell
end tell
Yes, you need System Events. It can give you all running and visible processes (i.e. = applications) and for each, list of windows.
Even more, you can then directly update position of each window. Here is sample script which gets all open windows of all applications and move the position of the "Library" Window of "Script Editor" (I open it for my tests):
tell application "System Events"
set theProcesses to application processes whose visible is true
repeat with aProcess in theProcesses -- loop thru all visible processes
set allWindows to every window of aProcess
repeat with myWindow in allWindows -- for each process, look for all windows
set Pname to name of aProcess
set Wname to name of myWindow
if Pname = "Script Editor" and Wname = "Library" then
set position of myWindow to {400, 900}
end if
end repeat -- window loop
end repeat -- process loop
end tell

exploring avaliable commands in an applescript program

I have an application and I can do the following command on (I know this because I googled for it):
tell app "TextMate" to reload bundles
What I would really like is to be able to ask the "TextMate" program:
tell app "TextMate" to list all commands
and have it list out all the things I can ask it to do:
... 'reload bundles', 'exit', 'open files'...
is there a way to do that with applescript?
The way to find all the commands of an app is to open its dictionary in your script editor. Usually "Open Dictionary ... " in the File menu, or drop the application onto the script editor.
[EDIT]
For applications that have AppleScript support, you can actually script opening the app itself with the Script Editor, a la:
set pathToApp to (choose file of type "APPL")
tell application "Script Editor"
open pathToApp
end tell
BUT this will be problematic with a non-scriptable app. You'll get an error, but Script Editor will actually open some part of the app (and it will be slow about it), then give you an unusable document. There's no way to catch this error. If you use the Smile script editor, you can use this method ...
set p to (choose file of type "APPL")
try
OpenDictionary(alias (p as string))
on error e
end try
... to open the dictionary of an app, and if it doesn't work (if the app doesn't have a dictionary), it returns an error but doesn't do anything else (but again, you can't catch the error and not have it complain, without hacking Smile)
[EDIT 2]
A rabbit hole to go down is trying System Events or the Finder to check for boolean of has scripting terminology property of a process, but I don't recommend it because I haven't found it to be reliable.
[EDIT 3]
Ach! I knew there was another method, but forgot what it was. As #mklement0 points out (thank you), you can do this to check for an app's script-ability prior to opening the app in Script Editor:
set pathToApp to (choose file of type "APPL") as text
set isScriptable to false
try
class of application pathToApp
-- only AppleScriptable applications have a class property
set isScriptable to true
end try
isScriptable

Can I manipulate the location of a dialog displayed through osascript?

I've been playing around with various UNIX commands and came across this one to display a dialog:
osascript -e 'tell app "System Events" to display dialog "Hello World"'
I'd like to change the position of the dialog. For most commands, I can just use man command to figure out how to do something for that command. However, man osascript doesn't tell me how I can change the position of the dialog box. How can I modify the command to put the dialog in a different place?
First, to get help with applescript just open the AppleScript Editor application and look under the help menu. The applescript language guide is in there and other tools. Also under the file menu is "Open Dictionary" which will show you the specific commands for applications.
To see the information about display dialog, open the dictionary of StandardAdditions and use the search field to find the command.
To answer your question, no, you cannot specify the location of a "display dialog" window. There are no parameters in that command for positioning the window, as you'll see in the dictionary. In addition, no other commands will help you either because you can't issue other commands while the dialog window is open because the code pauses while the window is open waiting for a response from the dialog window (like when you press the OK button).
If you need to set the position of a window to display information to a user then you'll need to use some other dialog tool other than "display dialog". You could create your own window in cocoa or google for some alternatives like cocoaDialog, SKProgressBar, and Notification Center.
There is a round-about way to go about this, which may be useful in some scenarios. Try the following:
on displayMessage(msg)
tell application "Finder"
activate
set c to (count windows)
ignoring application responses
display dialog msg with icon note
end ignoring
end tell
tell application "System Events"
tell application process "Finder"
repeat until ((count windows) > c)
delay 0.2
end repeat
set position of window 1 to {0, 22}
end tell
end tell
end displayMessage
displayMessage("I'm over here!")
Credit for this little script goes to a post here.
In implimenting this myself, I found it was limited to whether or not the application that is being called (Finder, in the example) supports the count window command (or whether it has API support at all).
I realise I've dragged up a question from 2013. I am posting this answer here in case it is of use to the OP or, more likely, someone else with a similar question.

How to check in AppleScript if an app is running, without launching it - via osascript utility

Consider the following AppleScript:
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
set safRunning to is_running("Safari")
if safRunning then
tell application "Safari"
-- Stuff I only want executed if Safari is running goes here.
end tell
return "Running"
else
return "Not running"
end if
The problem: when I run this via the osascript command line utility, if Safari is not running, it gets launched and the script reports "Running". This is not the behaviour I desire or would expect. Note that it works as desired/expected when run within AppleScript Editor.
Is this an osascript bug / known issue? Or is it somehow intended behaviour for reasons I'm missing? Can anyone get it to work as desired? (BTW I'm running OSX 10.7.5; I can't see how to get osascript to report a version number).
If you comment out the tell / end tell lines, it behaves as I'd expect: if Safari is not running, it doesn't launch it, and prints "Not running". So it seems to me like the tell is what's causing Safari to be launched, but it doesn't need to be actually executed, just present in the script...? For a while I wondered if maybe this was just how tell is supposed to work, but since it doesn't work like this in AppleScript Editor, I guess not...
In fact, here's another, madder, version with similar behaviour:
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
set safRunning to is_running("Safari")
return safRunning
if false then
tell application "Safari"
end tell
end if
This still always launches Safari, even though tell is inside an if false block after the return statement! (But again, this is fine in AppleScript Editor.)
BTW, this behaviour isn't limited to Safari, but it also isn't universal:
Affected apps include: Safari, TextEdit, iPhoto, AppleScript Editor, iTerm, ...
Non-affected apps include: Google Chrome, iTunes, Preview, Mail, Terminal, Address Book, Echofon, ...
So, does anyone have any ideas about how I might fix or route around this? Is it an osascript bug? Or am I missing something about AppleScript's semantics?
For context: I'm trying to write a script (to be embedded/called from some python) which queries open browsers for the URLs of any tabs they have open; I've got it all working fine except that it always launches Safari, whether it's open or not. I've boiled down that undesirable behaviour to the simple test case shown above. I'm not aware of any way to run this script from python without using osascript, other than appscript, which I don't want to use because it's no longer developed/supported/recommended.
Many thanks for all inputs / insights!
I suspect the reason you are getting this is because each time you call the script from the command line with osascript the script is being compiled.
The act of compiling on a tell application will afaik make the app launch.
Calling the script from the command line with osascript from a pre-compiled file i.e .scpt does not cause this behaviour because the is no compiling to be done.
But calling it from a plain text (.txt,.sh ) file will so the app will launch.
If you do not want to use a .scpt file and want to use a plain text file then you could try the trick of putting a run script command in the applescript.
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
set safRunning to is_running("Safari")
if safRunning then
run script "tell application \"Safari\"
open location \"http://google.com\"
end tell"
return "Running"
else
return "Not running"
end if
The script in the run script is only compiled when needed. You will need to escape any characters like quotes as in my example.
It will be easier if you write the script in a normal applescript document first and compiled it to check for errors.
Then copy it to the plain text file.
UPDATE **
The method I used above was from a old script I had used to solved this issue a while before I answered here.
The answer works and is not trying to be elegant. ;-)
I actually like user1804762 method below. As it does work but feel the Answer is not clear enough so I will give an example on using it.
set appName to "Safari"
if application appName is running then
tell application id (id of application appName)
open location "http://google.com"
end tell
return "Running"
else
return "Not running"
end if
This script can be run from the command line with osascript
example:
osascript /Users/USERNAME/Desktop/foo.scpt
Notice that the script is saved as a compiled script. This will work ok and you can also save and use it as a plain text script.
i.e.
osascript /Users/USERNAME/Desktop/foo.applescript
Some Info:
"Enhanced Application Object Model":
tell application "iTunes"
if it is running then
pause
end if
end tell
You can also do it that way:
if application "iTunes" is running then
tell application "iTunes" to quit
end if
You can also do this:
get name of application "iTunes"
get version of application "iTunes"
And to complete the journey:
get id of application "TextEdit" --> "com.apple.TextEdit"
tell application id "com.apple.TextEdit"
make new document
end tell
That was the "Enhanced Application Object Model". If an app still launches (for example, the first time you compile & execute the script) I assume it is because AS has to get some info from the app which it did not found in the dictionary (or something like that...?).
OK, I know this question is really old, but I stumbled on it looking for a different issue and had to pipe in considering how complicated some of these responses are.
The simple code to achieve what you want(ed) is:
tell application "System Events"
if application process "Safari" exists then
-- do stuff you want to do only if Safari exists
end if
end tell
On older systems, the syntax used to be:
tell application "System Events"
if exists of application process "Safari" is true then
-- do stuff you want to do only if Safari exists
end if
end tell
One of these should definitely work for you, intrepid searcher of Applescript solutions for action only when an app is running.
Oh! Bonus tip: And if you're not sure what the application process name is exactly (it is usually but not always the app name), before coding your final script run…
tell application "System Events"
get every application process
end tell
And find your app process name in the results.
Here's a screen grab of running that command. (Note the zillions of Google Chrome Helper instances. Thanks Google!)
HTH!
tell application "Finder"
set applicationsnames to get the name of every process whose visible is true
end tell
set appName to "Safari"
if applicationsnames does not contain appName then
say (appName & " is not running")
--add here what you want to happen
end if
return applicationsnames
This is returning {"Finder", "JavaAppLauncher", "firefox", "Microsoft Word", "iTunes", "AppleScript Editor"} for me
Hope this helps
All the previously made answers suffer from the same issue, though:
They look for the app by its name. However, the user may rename the app, and then the script will believe the app does not run, when in fact it does.
To properly check for a running app, it should be found by its bundle ID, which the user cannot change.
The bundle ID can be inquired with this command, for instance, when the app is already running:
tell application "System Events"
get bundle identifier of application process "Safari"
end tell
Or like this for any installed app:
get id of application "Safari"
To check whether an app with a particular bundle ID is running, use this code:
tell application "System Events"
set ids to bundle identifier of every application process
if ids contains "com.apple.safari" then
return "Running"
else
return "Not running"
end if
end tell
Furthermore, here's an example to check if an app is running, then quit it, then relaunch it, ensuring that the very same app is relaunched that was running before, and not some other copy that may also exist:
set bundleID to "com.apple.safari"
set apps to runningApps(bundleID)
set appCount to length of apps
if appCount is not 0 then
quit application id bundleID
repeat while length of runningApps(bundleID) = appCount
-- wait for the app to quit
end repeat
open first item of apps
end if
on runningApps(bundleID)
-- The try block is to catch the rare case of having more than one
-- copy of an app running at the same time. Unfortunately, in that
-- case this code will not run as expected, because we don't get the
-- correct list of multiple items back then. But at least the script
-- will not crash from it but handle it gracefully.
tell application "System Events"
try
return application file of (every application process whose bundle identifier = bundleID)
end try
end tell
return {}
end runningApps
I had the same problem as described here trying to set up an AppleScript (triggered by a BetterTouchTool gesture) that plays/pauses VLC or iTunes, but only iTunes if VLC is not running (due to my workflow) and, naturally, only VLC while it's running. (I use the automatic pause/play trigger for iTunes in VLC's settings, for launch and quit of the app.)
VLC was always launched on the first use of the BetterTouchTool-trigger after every relaunch of BTT as the dictionary-cache is deleted at that point and the AppleScript handler has to launch every scripted application if a tell is aimed at it in order to call its dictionary.
I didn't find anything that avoided this anywhere; there were some attempts, but none worked for me as the dictionary-call by the script handler is nothing we can influence. I came up with this dirty workaround:
Create a separate AppleScript file only containing the line that includes the tell for VLC
Save it at some place where it won't annoy you
Replace the line containing the tell in the original AppleScript with a line that runs that script
This will lead to the first compilation of the script not calling the application (VLC, in my case) directly, only the script, which means that the application will not need to launch.
VLC will need to launch once that separate file is called, but, well, if you call that file in order to tell VLC something, you will have VLC already opened (or will want it open) anyway.
The AppleScript I call through my BetterTouchTool-trigger (a specific tap on the trackpad, in my case) looks like this:
if application "iTunes" is running and not application "VLC" is running then
tell application "iTunes" to playpause
end if
if application "VLC" is running then
run script "/Users/jannis/bin/PlayVLC.scpt"
end if
The separate AppleScript file ("PLayVLC.scpt, saved in a folder called "bin" in my user folder which I created manually ages ago for such purposes) is just this:
tell application "VLC" to play
If you open that script manually, it will of course also launch VLC. But that hopefully won't be necessary often, or ever.
I actually have no idea if this creates any deeper problems I don't know of as I'm not a pro coder; if so, please notify me. I hope this helps anyone!

Create AppleScript for a program that isn't installed on the current computer

I'm trying to make two copies of an AppleScript, one that works for Entourage and one for out Outlook. I only have Entourage installed on the current computer.
According to the info on Microsoft's site, both applications have the same library of AppleScript commands, and I should be able to simply change the application name referenced within the script.
Changing:
Tell application "Microsoft Entourage"
to
Tell application "Microsoft Outlook"
Prevents me from saving the script because I don't have outlook installed on this computer. Is there any way around this? Do I need to use a text editor to edit the actual script file and change it there?
Thanks!
The following work-around may do the trick. On the computer where Entourage is installed, a using terms directive will let you compile the script, even if Outlook is not installed:
set theApp to a reference to application "Microsoft Outlook"
using terms from application "Microsoft Entourage"
tell theApp
get version
...
end tell
end using terms from
Upon compiling and saving the script the AppleScript Editor will bug you about the missing Outlook application, but it will nevertheless produce a compiled AppleScript file (.scpt).
Applescript is a pre-complied file format, meaning that every time you click "Save" it runs through a series of steps to ensure the script will work, but just short of actually running through the script's logic. Part of those steps is to look for the application to see if it exists on the Mac.
In short, if you want to save the script as an Applescript, you need the target application installed, otherwise you can save the script as a text file and move the file over to the target Mac to save as an Applescript over there.
It should be possible to make one script that works with both Entourage and Outlook, without bugging you if one isn't found either when you compile or when you run. I don't have either Entourage or Outlook but it should work like this:
using terms from application "Microsoft Entourage"
script theScript
tell application "Finder" to try
set theApp to application file id "Entourage's Bundle ID" as text
on error
set theApp to application file id "Outlook's Bundle ID" as text
end try
tell application theApp
-- do stuff
end tell
end script
end using terms from
store script theScript in "MyScript.scpt"
"using terms from" is only relevant when compiling the script - it isn't needed when running, though for some reason you'll still get bugged if that app isn't found. So by wrapping it around a script object and then writing out that script to file, the resultant script will still run but won't contain "using terms from" and so won't bug the user.
For getting a reference to the right app, Finder can look for it by ID and simply error if it isn't found rather than bugging the user. You'll need to insert the proper ID's there, I don't know what they are.

Resources