I have an automator workflow. How can I make it restart the workflow in an if statement?
on run {input, parameters}
if (input as text) is "" then
-- restart workflow
end if
return input
end run
Without AppleScript
At the end of automator workflow, put Loop. (Loop is in Library > Utilities) Loop has an option Ask to Continue. If the user click Continue, Automator will restart the workflow. If not, the next process after the Loop will be executed.
With AppleScript
At the end of automator workflow, put Run AppleScript and Loop. Set the Loop option to Loop automatically and Stop After 1000 times. The AppleScript code is as follows.
on run {input, parameters}
if (input as text) is "" then
tell me to quit
end if
return input
end run
The latter choice can repeat not more than 1000 times.
Related
How can I add a Finder Service that would use a single selected file, perform a shell script on it then display it as a dialog window?
I currently have Automator receiving files and folders within Finder then running an Applescript with the following:
on run {input, parameters}
tell application "Finder"
set filename to selection
set filename to quoted form of POSIX file filename
set theCMD to "/usr/local/bin/exiftool "
set theScript to theCMD & filename
set theResult to do shell script theScript
display dialog theResult
end tell
end run
I continuously receive errors. The intention is to show a dialog window with Exif metadata information for a single selected file within a Finder window. I used brew to install exiftool to retrieve the data.
I'm new to applescript and can't figure out how to get this to work. Any help is appreciated.
An Automator service passes input items to the workflow, so you don't need to do anything else to get the selection. In a Run AppleScript action the input parameter is a list, so you should pick a specific item or just loop though all the items:
on run {input, parameters}
repeat with anItem in the input
set anItem to quoted form of POSIX path of anItem
set theResult to (do shell script "/usr/local/bin/exiftool " & anItem)
display dialog theResult
end repeat
# return input
end run
Repeat scripts at same time in one script/application in Applescript
I have run into a problem while coding this troll program.
What I want to do is turn volume up without the victim to be able to turn it down and run a script at same time (in one script/application). I don't know how to combine these scripts into one and have them run at the same time. This is the script.
on run {input, parameters}
tell application "Google Chrome"
activate
open location "https://youtu.be/sZFOwzEFYRA"
repeat
delay 0.1
display dialog "Looooooolz" buttons {"Looooool", "Looolzz"}
if result = {button returned:"Looooool"} then
if exists window "https://youtu.be/sZFOwzEFYRA" then
open location "https://youtu.be/sZFOwzEFYRA"
end if
else
if exists window "https://www.youtube.com/watch?v=igfJIZayiy8" then
open location "https://www.youtube.com/watch?v=igfJIZayiy8"
end if
end if
end repeat
end tell
return input
end run
second script:
on run {input, parameters}
repeat
set volume output volume 100
end repeat
return input
end run
I don't think you can do that with pure AppleScript. However, you can get a similar effect using the combination of AppleScript and a shell script. Here's an example AppleScript that will display two dialogs in independent repeat loops. The "&" at the end of the first osascript command is the key to getting them running concurrently.
do shell script "#!/bin/bash
osascript -e 'repeat
display dialog 1
end repeat' &
osascript -e 'repeat
display dialog 2
end repeat'"
You should be able to extend this idea to have your two scripts doing whatever terrible things you want.
I have a shell script which calls an AppleScript. AppleScript runs some automated tests on a software with a given document.
After the AppleScript finishes its execution I am moving the document to some other folder using mv command in my shell script and then I am moving the next document so that AppleScript can run those tests with this new document. But as soon as AppleScript is called it moves the document to another folder without those tests being run on that document.
How can I give a wait in my shell script so that the document is moved only after the AppleScript has finished executing all the tests?
1. mv $file "/path/to/file" (Moving the document to the execution folder)
2. osascript /pathto/applescript.app (it will use this execution folder to run its tests)
3. mv "/path/to/file" /Users/Desktop/tempfolder (moving the document on which the test is completed to a temp folder)
Steps 2 and 3 go one after another without wait, hence the document on which the test is to be run is moved before the test completes.
Normally, shell commands are executed in-sequence. That is, the next statement is not run until the current one is done executing, whether or not it was successful. So if this is the case you can merely write your script one command after another.
I guess that osascript /pathto/applescript.app run some tasks in a different process, and that osascript returns from its own execution while the process doing "what you want" is still running.
What you could do would be to ps -aux | grep applescript.app then get the pid from that, and do a while loop afterward to see when the program cease. But that seem overengineered and you don't know the execution name of the applescript.app.
If I am wrong somewhere please anybody correct me
As Mayerz said, do shell script command is waiting for end of instruction to move to next AppleScript command. In any case, you can also use the "considering application response" bloc. Here is an example with a move in Finder :
set A to ("myVolume:Users:Me:Documents:sample.mov") as alias
set B to ("Users:me:Desktop:") as alias
tell application "Finder"
considering application responses
move A to B
end considering
end tell
display dialog "done"
Similar example with do shell script:
set A to ("myVolume:Users:Me:Documents:sample.mov") as alias
set B to path to desktop
set AU to POSIX path of A
set BU to (POSIX path of B) & "test2.mov"
tell application "Finder"
considering application responses
do shell script "mv " & (quoted form of AU) & " " & (quoted form of BU)
end considering
end tell
display dialog "done"
I need to write a simple applescript that will be opened with an argument. That argument will be a file path. I need it to save this file path to a text file. I want to avoid using shell script.
I'm finding this surprisingly difficult, despite having done it in .sh and .bat already
Thanks very much!
Adam
//// the current code which is not working is below. the code begins with "on run" and ends with "end run" but for some reason this isn't being highlighted as code
on run argv
set this_PATH to (POSIX file argv)
tell application "TextEdit"
activate
make new document
set text of document 1 to this_PATH as text
save document 1 in "/Users/adamparkinson/Desktop/date.txt"
end tell
end run
I made this work:
#!/usr/bin/osascript
on run argv
tell application "TextEdit"
activate
make new document
tell document 1
set its text to (item 1 of argv as text)
save in posix file (item 1 of argv as text )
end tell
end tell
end run
I called it like this:
./SavePath.sh /Users/Me/Desktop/junk/Newstuff.txt (Of course I used chmod u+x SavePath.sh to make it executable.)
If this works for you, then please check of my answer as answered. :)
with applescript it is very easy to create application Drag and Drop without any script shell below a version Drag and Drop of your script
on open draggedItems
set this_PATH to quoted form of POSIX path of draggedItems
repeat with currentItem in draggedItems
tell application "TextEdit"
activate
set doc to open POSIX file "/Users/adamparkinson/Desktop/date.txt"
make new word at end of text of doc with data (this_PATH as text) & return
end tell
end repeat
end open
I have this automator setup which allows you to open a folder with terminal. The thing I want is that after the folder is opened via terminal, it should run the compass watch script.
This is what I currently have but I don't seem to find the solution myself. I tried adding some applescript and some bash, but nothing seems to work. Anyone know the solution?
Try this :
Add the "Run AppleScript" action in your workflow
If the compass watch script is a file, use this AppleScript script
on run {input, parameters}
set compasswatchScript to quoted form of "" -- drag drop the script file between double quote
tell application "Terminal"
do script compasswatchScript in window 1
end tell
end run
Insert the path of your script file in the second line of this script
--
if compass watch is a command, use this AppleScript script
on run {input, parameters}
tell application "Terminal"
do script "compass watch" in window 1
end tell
end run