I have a bit of AS that opens a dialog box and prompts a user for an input, with button options of "cancel" and "send" ("send" is the default button). As it's the default button, "send" activates when I hit the enter/return key on my keyboard. I would like to have "cancel" activate when I hit the escape key on my keyboard. Is there a way to link a keyboard key with a button? I'm looking for a coded solution as opposed to an application setting solution because this piece of AS will be run by another application.
Any help is appreciated, example code is below!
set returnedThings to (display dialog ":message:" default answer "" with icon note buttons {"cancel", "send"} default button "send")
set theMsg to text returned of the returnedThings
set theBtn to button returned of returnedThings
if theBtn is "send" and theMsg is not "" then
-- do application specific tasks
end if
Cancel is linked to the ESC key automatically if it’s capitalized.
Related
enter image description here
I have the window of the picture above, save the configuration through stand-alone save, AppleScript can not click Save through click, the following script returns missing value, please ask in AppleScript, how to click static text
tell application "System Events"
tell process "WeChat"
click menu item "Proxy Settings..." of menu "WeChat" of menu bar 1
click radio button "Don't Use" of window "Proxy Settings"
delay 1
set focused to true
click static text "Save" of window "Proxy Settings"
end tell
end tell
```[enter image description here](https://i.stack.imgur.com/ZWJ9V.png)
tell application "System Events"
tell process "WeChat"
click menu item "Proxy Settings..." of menu "WeChat" of menu bar 1
click radio button "Don't Use" of window "Proxy Settings"
delay 1
set focused to true
click static text "Save" of window "Proxy Settings"
end tell
end tell
I don't have/use WeChat, so cannot examine the UI to confirm here, but static text elements—generally speaking—don't often have actions attached to them ("AXPress" is the name of the action that the click command aliases). From a design perspective, static text elements are intended as an inert label for some other UI element that plays a functional role. In this case, it looks like a button element, and its name is probably also "Save". button elements are also good candidates for attaching a whole bunch of actions to.
Therefore, instead of:
click static text "Save" of window "Proxy Settings"
you could try:
click button "Save" of window "Proxy Settings"
If that throws an error, it'll either be because the button element's name is not "Save", or the element is not a button. But you can find out what it is like this:
-- click button "Save" of window "Proxy Settings"
tell window "Proxy Settings" to get (the last UI element ¬
where the name of its actions contains "AXPress")
This will yield a reference to the object you're trying to click, by virtue of its actions necessarily containing one named "AXPress" (i.e. it responds to the click command), and its physical positioning almost certainly inferring it to be the last such UI element.
"Save" is not a button, and only one button can be found through the UI element, this button is a close button, and nothing else.
I have a simple script that automates the command "Export Library" in Music. It does the following:
tell application "System Events"
tell process "Music"
set libmenu to menu item "Library" of menu "File" of menu bar 1
click menu item "Export Library…" of menu of libmenu
tell window 1
click button "Save"
tell sheet 1 to click button "Replace"
end tell
end tell
end tell
(So basically it opens the menu and clicks the obvious buttons, saving me a few clicks). However, after upgrading to Ventura (macOS 13.0), this stopped working. The command click button "Save" is failing with:
Can’t get button \"Save\" of window 1 of process \"Music\"."
I tried to say click button 1 or click button 2 instead, but that doesn't work. I then said name of every button and it printed
{missing value, missing value, missing value}
I couldn't find a good manual for AppleScript, but it does look like something changed in Ventura. Any hints will be appreciated!
Thanks,
OK, I replaced click button "Save" with:
repeat with b in (get buttons)
if name of b is "Save" then
click b
end if
end repeat
Interestingly, I can still click the "Replace" button directly without resorting to a loop.
Thanks!
OK! I found a hint for the answer here: Just add a short delay before trying to click on "Save"!
delay 0.5
click button "Save"
Without the delay, the the dialog doesn't seem to be ready and indeed if you do get buttons you will only see the 3 from the top left corner (close, minimize, zoom). With the delay, get buttons returns 6 buttons, including the "Save".
I'm looking for assistance with passing a variable from AppleScript to Automator.
I am working on a small automator app. I need to type a number that will be added to the end of a PDF's name. I used "Ask for text" in automator, however, I found out that, first, the pop-up window does not stay at the centre of screen. (27" iMac.), and second, every time after I input a number, I need to use mouse to click OK, the Enter key does not work.
So I turned to AppleScript for help. And here is what I found, a window that let me input the number, then click continue, then pass the number to Automator to add to PDF's name.
Set Value of Variable to storage
Run AppleScript: Ignore this action's input.
on run {input, parameters}
display dialog "Please enter QC Round." default answer "" with icon stop buttons {"Cancel", "Continue"} default button "Continue"
--> Result: {button returned:"Continue", text returned:"MySecretPassphrase"}
return input
end run
Set Value of Variable to Continue
Get Value of Variable to Storage
Rename Finder Items: Add Text
Add:_QC_Continue(Variable) after name
But it does not work. Can anyone help me fix it?
In Automator, an action gets its input from the previous action, and passes its results on to the next action. In a Run AppleScript action, the input is in the input argument to the run handler, and items returned by the handler are what get passed on.
In your example, you are just passing on the input, which doesn't have a value since the action is ignoring its input. The solution is to return the text from the dialog, for example:
on run {input, parameters}
set dialogResult to (display dialog "Please enter QC Round." default answer "" with icon stop buttons {"Cancel", "Continue"} default button "Continue")
return text returned of dialogResult
end run
How to click "Go" button on go to finder dialog by applescript?
I have below code: "dialogObj" is the dialog of Go to Finder dialog, but the "click button 1 of searchFor" does not work in here.
-- Get the search for pop-up object
set searchFor to first sheet of dialogObj
-- Select all the text in the search field and press delete
key code 0 using command down
key code 51
delay 0.5
--Paste file name
keystroke "v" using {command down}
delay 0.5
-- Click the 'Go' button in the search for pop-up
set textField to value of first text field of first sheet of dialogObj
*click button 1 of searchFor*
delay 1
In macOS Sierra the text field has been replaced with a combo box:
Try
tell 1st sheet of dialogObj
set textField to value of combo box 1
click button "Go"
end
By the way: If you select the entire text you don't need to press Delete. Any pasted or typed text overwrites the selection.
You asked: "How to click "Go" button on go to finder dialog by applescript?"
Did you mean "Go to Folder" dialog, like this:
If so, then a simple RETURN will "click" the Go button:
tell application "System Events"
key code 36
end tell
I'm making a text game but can't get it to end the script half way through when the user pushes quit. I have tried quit, return and error -128 but none of them work.
The code isn't inside a tell/end tell because I'm not wanting to use an application, but I've tried putting it inside one with finder and that didn't work either..
Any help is appreciated :)
set temp to display dialog "Welcome" buttons {"Play", "Quit"}
if temp = "Quit" then
error number -128
end if
set temp to display dialog "What is your name?" default answer "Joe" buttons {"Submit"}
set userName to text returned of temp
etc. etc.
Another solution: define the cancel button.
display dialog "Welcome" buttons {"Play", "Quit"} cancel button "Quit"
-- no need to check the button returned, the script quit automatically when user cancelled
set temp to display dialog "What is your name?" default answer "Joe" buttons {"Submit"}
set userName to text returned of temp
etc. etc.
You should get button returned:
if button returned of temp = "Quit" then
error number -128
end if