I need help writing an applescript to run within automator. All it needs to do is It output "This is my document" to the next automator action.
Sometimes it's best to show with an example. Add two appleScript tasks to the Automator window and paste in this code into the first one:
on run {input, parameters}
set sentence to "This is my document"
return sentence
end run
... this this into the second one:
on run {input, parameters}
repeat with sentence in input -- beacause it could be more than one
display alert sentence
end repeat
end run
When you hit run (top right corner to run them both in sequence) you'll see that the first script successfully outputs the sentence, and that the other one is showing it with an alert. Hope this answer you question.
Related
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 ... )
I have a bunch of files within a folder that I'd like to periodically delete the oldest 3 files in it. The problem is I can't rely on "creation date" or "last modified date" on these files (long story). These files all have the same name with appended timestamps -- xxxxx 2019-08-29-_01-37.sparsebundle
I've created Automator steps as shown
here.
Now after the "Sort Finder Items" step, how do I use Applescript to select and delete the bottom 2 or 3 files?
Any help would be much appreciated.
To move to the Trash, or permanently delete, the last three items in the list returned from the Sort Finder Items action, add a Run Apple Script action with the following example AppleScript code:
on run {input, parameters}
try
repeat with i in items -3 thru -1 of input
# To move to Trash, use Finder.
-- tell application "Finder" to delete alias (i as text)
# To permanently delete, use System Events.
-- tell application "System Events" to delete alias (i as text)
end repeat
end try
end run
Remove the -- from in front of the tell application ... statement for which action you wish to take.
If you only want to work with the last two items in the list then change -3 to -2 in: repeat with i in items -3 thru -1 of input
Note that this example AppleScript code worked for me in macOS High Sierra.
Note: The example AppleScript code is just that and does not contain any aditional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
I'm trying to automate the click in a certain position of the screen. I found several answers to my question, but none of them helped me.
This command should verify if "Spotify" (it's a random application, just to do a test) is frontmost. If that's true, it should click on a certain position of the screen (in that case, the position of the mouse is on the "play" button).
I run the program, everything is going ok. But when i put the application in foreground nothing happen. The program end with "Result: 1"
set x to 0
repeat until x is equal to 1
tell application "System Events"
if frontmost of application "Spotify" is true then
delay (2)
tell application "System Events"
click at {720, 634}
end tell
set x to 1
end if
end tell
end repeat
I also tried to put some "delay", but they've been useless.
Found an alternative way: use python.
To install python, just surf the internet and you'll find out how to do it.
The library I used is "pyAutoGui".
To install it just look here:
https://pyautogui.readthedocs.io/en/latest/install.html
Then, if you wanna use python and this library on automator, do like this.
Open automator
Find in the menu "Run Shell Script"
On "Shell" option, select "/bin/bash"
Then, if you wanna run your program with a third part library, copy that
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7<< "EOF"
insert your python program and then write "EOF" at the end
Here a little exemple
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7<< "EOF"
import pyautogui
pyautogui.click(720, 634)
EOF
OK total newb here but this should be pretty simple.
I want to make a little automator script to make a "new day one entry" in my services menu on mac.
So I have text, I need to select this text and then have automator run the keystroke Command+D
Please help!
So far I have the code for the keystroke, I just can't figure out how to make the Automator function "Get Specified text" text to become "selected" so that I can run the applescript.
Here is my automator script so far
You should follow the instructions here: https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/MakeaSystem-WideService.html
First problem I see is that you don't seem to have chosen a document type of "Service" (in Automator). (You should see 'Service receives selected ...' in the title bar.)
You'll need this structure for your AppleScript even though you are not processing the input (the text you have already selected which is picked-up by the system as selected not as a specific parameter for the script).
on run {input, parameters}
tell application "System Events"
keystroke "d" using command down
end tell
end run
I don't have Day One, so I can't tell you what's correct, but it does not appear that Cmd-D is the correct code for a new entry from what I have found on the web.
Finally, if you are substituting a service to select from a contextual menu is that quicker than just doing the keyboard shortcut?
I'm creating an automator pdf print plugin.
When you choose the print plugin the filename to the pdf is the input (normally /var/something /documentName.pdf)
I would like to get the documentName to use it later in an Rename Finder Item.
I'm using atm applescript to accomplish this.
on run {input, parameters}
tell application "Finder"
set fileName to name of ((POSIX file input) as alias)
end tell
return fileName as string
end run
The problem is that this only works when I put an Ask for Text Action before the applescript which displays the posix path.
If I remove the Ask for Text action the applescript fails.
The workflow is at https://www.dropbox.com/s/jp4t9pen3gvtyiq/Rename-Action.workflow.zip
I guess it is something simple but this is the first applescript / automator workflow I'm creating.
As I fail on commenting
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken post below.
Thanks!
I created a test workflow with this AppleScript:
on run {input, parameters}
tell app "System Events"
display dialog ((class of input) as string)
end
return input
end run
That displayed "list". I then modified it to:
on run {input, parameters}
tell application "System Events"
display dialog ((class of first item of input) as string)
end tell
return input
end run
That displayed "alias".
So, the input to a PDF workflow is a list of aliases. Write your script with that in mind and it should work. For example, this works:
on run {input, parameters}
tell application "System Events"
display dialog ((name of first item of input) as string)
end tell
return input
end run
When working with AppleScript, it can really help to forget everything you know about file paths. If you think in paths, you will be doing path-math in your head and all of it is unnecessary work. What you want to work with is objects. When doing file operations, you work with alias objects.
If you look at the PDF you are working with in Finder, and go File ▶ Make Alias then you’ll create an alias file. You can drag that alias file all around the file system of the disk it is on, put it in any folder, and when you open the alias file, it will still always open your original PDF, even if you forget what path name your original PDF file has, and even more importantly: the alias will open the PDF even if the PDF has moved to somewhere else in the file system. An alias does all that work for you. You don’t need to know the path names.
In AppleScript, rather than working with files, you work with aliases, and whatever you do to an alias is also done to the original file. So you don’t need to know the path name of a file to change its name — you only have to have an alias of it to work on. You store that alias in a variable.
So what you want to do is set the input PDF alias to a variable, and then later, that variable is what you give Finder to rename. You don’t have to know any paths. It doesn’t matter where the input PDF is stored on the file system — the alias will take care of that.
Here is an example AppleScript that demonstrates the principle of taking an alias as input, and then later renaming that alias (and thus, the original file:)
tell application "Finder"
set theInputFile to (choose file)
-- do a workflow here
set the name of theInputFile to "Renamed" & "." & the name extension of theInputFile
end tell
Here is a line-by-line description of the above script:
the opening tell block that specifies we are talking to Finder
show the user a choose file dialog box, and set the alias that is returned by that dialog box to a variable called “theInputFile”
a comment that is a placeholder for whatever workflow steps you might want to do
rename theInputFile to “Renamed” and its original file extension
quit talking to Finder
And even where you want to work with the folder that contains your original input file, or want to know what disk the input file is on, you still don’t need to work with path names:
tell application "Finder"
set theInputFile to (choose file)
set theContainingFolder to open the container of theInputFile
set theInputFileDisk to the disk of theInputFile
end tell
And if you want to know what kind of file the input file is, you don’t have to look at the filename extension and figure it out, you can just say:
set theInputFileKind to the kind of theInputFile
if theInputFileKind is equal to "Portable Document Format (PDF)" then
-- do stuff
end if
And if you want to work in specific folders, such as the home folder, there are special properties for that, like “the path to the home folder” so that the following script opens “~/Public/Drop Box” on any system, no matter what the user name:
tell application "Finder"
activate
set theHomeFolder to the path to the home folder as alias
set theDropBoxFolder to folder "Drop Box" of folder "Public" of theHomeFolder
open theDropBoxFolder
end tell
You can walk around disks and folder structures as objects as shown above, so again, there is no need to think in paths. Think in terms of setting variables to objects that you want to interact with.
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken's post
Thanks!