How to pass variable from AppleScript to Automator? - macos

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

Related

How to pass value from apple script to shell script in Automator?

I'm planning to run a shell script followed by a AppleScript. But I would like to know if it is possible to pass a parameter value to shell script as follows.
I'm getting some weird error which I do not understand. Can someone please help me understand the error and provide a way to pass value (Continue/Don't Continue) to Shell script from Apple Script?
Code in Text format:
on run {input, parameters}
set theDialogText to "By running this script, it will update your hosts file. Would you like to continue?"
set isupdate to display dialog theDialogText buttons {"Don't Continue", "Continue"} default button "Continue" cancel button "Don't Continue"
return {isupdate}
end run
First of all please post text, not an image.
The error occurs because display dialog returns a record (in terms of Objective-C a dictionary) but the parameter must be a string.
You have to write
set isUpdate to button returned of (display dialog ... )

Dialog "cancel" button linked to escape key

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.

AppleScript ASObjC Runner Not Displaying Progress Of Shell Script

Ok, so I have the progress bar made, using ASObjC Runner, but it doesn't show the progress of the shell script, and also doesn't hide the progress bar after the shell script completes. Anyone know why?
Here is the code now:
display alert "You may have to restart your computer after using this tool!" buttons {"Ok"} default button 1
set question to display dialog "RMR (Remove My Redirect)
Are you unable to go to a website at home because of that annoying St. Bernard Redirect?
If the answer is Yes, then RMR is your solution! Simply Choose Remove to remove it, and Replace to put it back." buttons {"Remove", "Replace", "Erase Evidence"} default button 3
set answer to button returned of question
if answer is equal to "Remove" then do shell script "mv /Library/LaunchDaemons/com.stbernard.rfcd.plist ~/"
if answer is equal to "Replace" then do shell script "mv ~/com.stbernard.rfcd.plist /Library/LaunchDaemons/"
if answer is equal to "Erase Evidence" then set question to display dialog "Are you sure? RMR will be deleted forever." buttons {"Yes", "No"} default button 2
set answer to button returned of question
if answer is equal to "No" then do shell script "echo"
if answer is equal to "Yes" then ¬
tell application "ASObjC Runner"
reset progress
set properties of progress window to {button title:"Cancel", button visible:true, message:"Removing...", detail:"Please be patient", indeterminate:false, max value:100, current value:0}
activate
show progress
end tell
repeat with i from 1 to 100
do shell script "srm -rf ~/Downloads/RMR.app; history -c; killall Terminal"
tell application "ASObjC Runner"
activate
set properties of progress window to {detail:"Progress: " & i, current value:i}
if button was pressed of progress window then
exit repeat
end if
end tell
end repeat
tell application "ASObjC Runner" to hide progress
Thanks in advance!
Also, here is a link to the app in case you need it: RMR
Two thoughts: 1. There for sure are issues with do shell script being synchronous/asynchronous. Since you hand out the work load, not sure what the pb does with that. 2. Start off by stripping out the guts of your script, leaving just the flow, the dialogs and the if clauses. Replace all the stuff (do shell scripts, etc) with delay statements, in order to first test your pb.
However, you're looking for |answer| being set to "Yes", but it will never be set to "Yes" in your script. That's not an option in the dialog box.??? You need to reduce and test parts of your script bit by bit.
I would also avoid using that helper app, which may be part of your problem. You can do a progress bar without it, as you can see in the answer to your previous question: stackoverflow.com/questions/27517042/…
– jweaks

Force quit on Applescript

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

Input Dialog Box Reappears when it is "on idle" in Applescript

I have this functionality in my Applescript wherein a input dialog box is shown to the user to enter some text. And this dialog box code is inside "on idle" "end idle" which repeats after every 3 seconds.
The issue is when this dialog box is shown and the user doesn't enter any details and leave the dialog box open, than after a minute or so this dialog box still remains but another dialog box appears (the same one repeats). How should I handle this issue inside "on idle" anyone?
Breakup of the code is shown below for reference.
on idle
try
tell application "iTunes"
repeat
set loginbutton to display dialog "Enter your facebook log in name to start using XXX." default answer loginusername with title "XXX Log In" buttons {"Quit", "OK"} default button 2
display dialog "loginbutton = " . loginbutton
end repeat
end tell
end try
return 3
end idle
In regular AppleScript, when you put up a dialog the script will wait until the dialog is dismissed before continuing. I am not able to get the symptoms you are describing, although your example snippet is incomplete and a bit buggy - you are in a repeat (forever) loop with no means of escape, since you are also trapping all errors.
The idle handler isn't really the place for things like this - this handler is called when your application is, well, idle, so whatever code is in it will run every time the script isn't doing anything.
if you are just wanting a dialog that repeats until a correct answer is returned, you can use something like the following in your main run handler
repeat -- forever
display dialog "this is a test, so enter something with \"test\"" default answer "test"
set theAnswer to text returned of the result
if theAnswer contains "test" then exit repeat -- success
end repeat
log theAnswer
Note that although a dialog's cancel button generates a "user cancelled" error, in a stay open script the script won't quit on the error, so you will need to do your own error handling.

Resources