How can I move a dialog window in Applescript? - applescript

I've been told that, technically, you can't move a dialog in AppleScript. Amazingly, the following code works.
I'm running a script for InDesign that creates a dialog box. Opening a new AppleScript Editor window and running this code will move the dialog box:
tell application "System Events"
tell application process "Adobe InDesign CS6"
set position of window 1 to {1000, 400}
end tell
end tell
The problem is: I can't figure out a way to incorporate this code directly into my original script -- it just doesn't work, no matter what I try or where I insert it into the code. I have to run it from AppleScript Editor
Any ideas?

Dialog boxes are usually blocking in the host application. You can certainly tell System Events to move a window, but Adobe InDesign itself is busy throwing up a blocking dialog box at the time, so it can't move its own dialog box.
If you want to get clever, you can fork off a process at the correct time and with the magically correct delays to move your dialog for you. This would be something like
do shell script "osascript -e 'delay 4' -e " & (quoted form of "tell application \"System Events\" to tell application process \"Adobe InDesign CS6\" to set position of window 1 to {1000, 400}") & " > /dev/null 2>&1 &"
Customize the delay as needed. This will be a magical value you that will have to figure out. Depending your needs, you might be able to put in a test to see if the dialog has disappeared before the script continues.
The bit at the end, > /dev/null 2>&1 &, tells bash to redirect stdout to /dev/null (junk it) and redirects stderr to stdout, then tells the whole command to run in the background. Without this entire chunk, this statement will block AppleScript from continuing until the shell script finishes executing.

Related

How to write apple script code to automate daily small task?

I have found the applescript code on internet, to start a terminal and launch Elasticsearch
the code is
tell app "Terminal"
do script "elasticsearch-5.5.0/bin/./elasticsearch"
end tell
It works fine,
now I want to add more stuff on it, I need to open 4 more new tab not new window just tab (command + T). and then run different command such as log tail command, start kibana like one after another in each tab.
I am new to applescript and got tired by searching samples and tutorials, can anyone suggest a solution or your idea to achieve the automation.
Terminal's scripting dictionary isn't very good — a lot of 'Apple Event Handler Failed' errors — but you can manage it with code like the following:
tell application "Terminal" to activate
my makeTab("ls -al")
my makeTab("top")
my makeTab("cd ~/Documents")
on makeTab(cmd)
tell application "System Events" to keystroke "t" using {command down}
tell application "Terminal"
do script cmd in last tab of front window
end tell
delay 0.2
end makeTab
Just put whatever commands you want run inside the makeTab() call.

applescript: how to terminate an executing terminal

I'm trying to make an apple script to allow me to manage a cpp project automatically. And I used Jenkins too.
Basically, the cpp project is a kind of infinite loop, which will execute in a terminal. And I set Jenkins rebuild and execute it per 20 sec. In other words, my pc should do the tasks below:
1) rebuild the project;
2) execute the project;
3) wait for 20 sec;
4) close the terminal.
However, when I arrive at 4), a dialog will pop up: Do you want to close this window?
So I have to click the button "Close".
My question is: how to code in applescript to do the 4) automatically?
Here is my applescript:
tell application "Terminal"
do script "cd /Users/Shared/Jenkins/Home/workspace/test"
do script "make clean" in window 1
do script "make" in window 1
do script "./matrix.out" in window 1
delay 20
close window 1 # problem's here
end tell
However, when I arrive at 4), a dialog will pop up: Do you want to close this window? So I have to click the button "Close".
But you can do that using AppleScript too (via GUI scripting thru System Events), so nothing you've said is a problem so far.
tell application "Terminal" to activate
tell application "System Events"
tell application process "Terminal"
tell its window 1
tell its sheet 1
click button "Close"
end tell
end tell
end tell
end tell
I wonder if this might help:
Running process in background after closing terminal
The problem might be that what you're lastly running in the terminal is a process (matrix.out) that will get killed by closing the window, and you don't want that, right? So it may be that you need to alter the matrix.out script using the technique described (if possible) to keep it from doing that.
It may also be possible to solve your problem by making matrix.out executable (chmod it to executable or use Kilometre) and just "open it" using vanilla applescript.

How can I use AppleScript to address dialog box in Ableton Live?

I have a collection of Ableton Live files (extension ".als") that I need to cycle through while playing a show. I'd like to dedicate a keyboard shortcut to launch each one, and had intended to use AppleScript for this.
The issue is that each file gets changed as I go through the process of playing the associated song, so that when I press the keyboard shortcut to launch the .als associated with the next song in my set, Ableton opens the "Save changes before closing?" dialog box (at which point what I want to do is select "Don't Save").
Simply pressing command + D at this point will do the trick, but I'd really like to automate this keypress. I can't seem to figure out how to get applescript to do this. I'm an applescript noob, and clicking the "Open Dictionary" option in AS seems to show that Ableton is not officially a scriptable app.
Any thoughts on this? Here's an example of an AppleScript I've been trying. This starts the process of opening the next .als in my set list, but won't click the "Don't Save" button.
tell application "Finder"
activate
open document file "Song 1.als" of folder "Desktop" of folder "User" of folder "Users" of startup disk
end tell
tell application "System Events"
keystroke "d" using command down
end tell
Interesting!
Finally came across tips that made it work:
Add both the Script Editor and Ableton Live to the Accessibility API:
System Preferences > Security & Privacy > Privacy...
Ignore application responses to continue the script during dialog.
LiveLoader.scpt:
-- open file
ignoring application responses -- don't wait for user input
tell application "Ableton Live 9 Suite" to open "Users:username:Desktop:LiveSet Project:LiveSet.als"
end ignoring
-- use delay if needed
-- delay 0.5
-- skip saving file
tell application "System Events"
set frontmost of process "Live" to true
key code 123 -- left
key code 123 -- left
keystroke return -- enter
end tell
Note:
Consider possible security impact.
Perhaps simply disable apps in Privacy List after use. (Could be scripted ;)
Can now also send mouse clicks, for more creativeness. :)
I know this is old. but in the interest of helping others who might find themselves here... heres what i have done.
use a program call Qlab. the free version will be fine.
make an applescript Cue. go to the 'trigger' tab. select midi trigger. hit the midi key you would like to assign the command too. this cue will now launch when it receives this midi note - even when running in the background.
go to the 'script' tab. copy and paste the script below.
you can make the relevant adjustments for each song. Basically each key will close all current ableton files without saving - as requested. and then launch a specific live set. which ever one you have assigned. in this case, the song 'Less Than Nothing'
the code...
tell application "System Events"
set frontmost of process "Live" to true
keystroke "q" using command down
tell application "System Events" to keystroke (ASCII character 28) --left arrow
tell application "System Events" to keystroke (ASCII character 28) --left arrow
keystroke return
end tell
delay 2.0
do shell script "open '/Users/CamMac/Desktop/Less Than Nothing 2 .als' "

Bypass “Can't get window” error in AppleScript

I'm trying to make a bash script to close certain finder windows (I'm on MacOSX). Unfortunately, the script terminates as soon as the first window is found to not be open. (ex: No window titled "Communication" open, yet window "Editors" is open; No window is closed). If I open a window titled Communication, it does close, but nothing after the first command fails. I've tried exit and on error, and taking out "set -e", but nothing seems to be working. Here is my script:
#!/bin/bash
set -e
osascript <<EOF
tell application "Finder"
close window "Communication"
close window "Editors"
close window "Gaming"
close window "Music"
close window "Technical"
close window "Text Editors"
close window "Utilites"
close window "Camera"
close window "External"
close window "TAB Actual"
end tell
It gives me
error: 24:57: execution error: Finder got an error: Can't get window <"first window found to not be open">. (-1728) (1)
I don't know if this means anything, but the code is being run through Automator.
Thanks to anyone that can help me, and yes, I am very new to bash.
You can use the ignoring application responses statement, for example:
#!/bin/bash
set -e
osascript <<EOF
tell application "Finder"
ignoring application responses
close window "Communication"
close window "Editors"
# More windows here...
end ignoring
end tell
More details about control statements in the Applescript Language Guide: https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_control_statements.html
This is what try statements are for:
set windowNames to {"Communication", "Editors", "Gaming"}
tell application "Finder"
repeat with wName in windowNames
try
close window wName
end try
end repeat
end tell

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.

Resources