I've created two droplets, one to rename files, another to print files. They are more complicated than that, but this is the essence. Sometimes we need to just rename them, sometimes just print them, and on occasion do both. I'd prefer to keep the two droplets separate because of extensive customization required for each user.
Desired workflow: drag file to RenameMe droplet, if command key is held down then pass the renamed file to the PrintMe droplet.
With the help of the checkModifierKeys script (sorry, don't have the citation handy) I can check to see if the command key is pressed, so that part of the script is taken care of. The problem is how to trigger the second droplet from the first. I've tried the opening the file with the second droplet as the application (as in the code below) but get a communication error.
Any ideas?
--Alex
Sample code:
on open the_Droppings
set flPth to POSIX path of (path to me) & "Contents/MacOS/checkModifierKeys"
set cmdPressed to (do shell script (quoted form of flPth & " command")) as integer as boolean
repeat with i from 1 to (count of items in the_Droppings)
set file_name to "NEW NAME FROM SCRIPT" #actual script that generates name isn't relevant
tell application "Finder"
set name of file (item i of the_Droppings) to file_name
end tell
if cmdPressed is true then
#pass the file to the PrintMe droplet
tell application "PrintMe"
open (item i of the_Droppings)
end tell
end if
end repeat
end open
You can add an explicit run handler to PrintMe, which will allow you two different entry points into the script. Both of which take arguments. I've set it up here with one file being passed to the run handler, and a list being passed to the open handler, but if you wanted you could pass a list to the run handler and repeat the same way you do in open.
In RenameMe:
if cmdPressed is true then
#pass the file to the PrintMe droplet
run script (load script file "path:to:PrintMe.app") with parameters (item i of the_Droppings)
end if
In Print Me:
on open the_droppings
repeat with i from 1 to (count the_droppings)
process(item i of the_droppings)
end repeat
end open
on run the_file
process(the_file)
end run
on process(the_file)
// Code for printing files goes here
end process
Related
I am working on an Automator Application (drag and drop) to rename some files. One of things I want to do is strip out the "where from" of the zip file. This script works, except it asks for the file when run vs allowing me to just drag/drop the file on top of the app icon.
What am I doing wrong? How do I make the application drag/droppable?
on deleteWhereFrom(fileToClean)
try
set posixPath to quoted form of POSIX path of fileToClean
do shell script "xattr -d com.apple.metadata:kMDItemWhereFroms " & posixPath
end try
end deleteWhereFrom
on open zip
repeat with i in zip
deleteWhereFrom(i)
end repeat
end open
on run
set zip to choose file with multiple selections allowed
repeat with i in zip
deleteWhereFrom(i)
end repeat
end run
In an AppleScript application, the open handler is used to get items dropped onto the droplet. In an Automator application however, items dropped onto the application are passed on to the first action in the workflow, which does whatever it does and passes its result to the next action in the workflow, and so on.
For the Run AppleScript action, its input is in the input parameter to the run handler, which is a list of items passed from the preceding action, and when the run handler finishes doing whatever it does, the result returned is what gets passed on to the following action.
In your original sample, the problem is that the Run AppleScript action is not using any of the items that are being passed along in the workflow, but is instead using choose file to ask for the items to use (again). Your script should be:
on run {input, parameters}
repeat with i in input
deleteWhereFrom(i)
end repeat
return input -- return the input items to any following actions
end run
on deleteWhereFrom(fileToClean)
try
set posixPath to quoted form of POSIX path of fileToClean
do shell script "xattr -d com.apple.metadata:kMDItemWhereFroms " & posixPath
end try
end deleteWhereFrom
Note that depending on any previous actions you are using, you may need to use a Set Value of Variable action at the beginning of the workflow to save the original items (the ones dragged onto the application), and then use a Get Value of Variable action (setting its "Ignore this action's input" option as needed) to get those original items in order to pass them on to the Run AppleScript action.
I'm trying to create a context menu shortcut to open a file/folder in VS Code from the original item or its alias
So far I was able to create an Automator Service, which:
receives selected: files or folders
in: any application run
shell script:
open -n -b "com.microsoft.VSCode" --args "$*"
How can I change it to accept also aliases?
Symbolic links should be OK, but Finder aliases usually don't work, since most shell utilities see them as small data files and don't know how to interpret them. One solution would be to add a Run AppleScript action to look for aliases in the input and use the original item instead, for example:
Service receives selected files or folders in any application
Run AppleScript:
on run {input, parameters}
set output to {} -- this will be a list of the output items
tell application "Finder" to repeat with anItem in the input
if anItem's kind is "Alias" then
set the end of output to POSIX path of (original item of anItem as alias)
else
set the end of output to POSIX path of anItem
end if
end repeat
return output
end run
Run Shell Script, etc
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'm trying to make a droplet script in Applescript using the code below.
I have a handler which runs a command line with the POSIX path of the selected file.
When I run the script everything works (my command line handler works fine). But, if I drop the file to my script, nothing happens.
I need some help, please
on run
set thePath to POSIX path of (choose file of type "img" default location (path to downloads folder from user domain))
doIt(thePath)
end run
on open myImageFile
tell application "Finder"
if (count of myImageFile) is greater than 1 then
display alert "A message" as critical
else if name extension of item 1 of myImageFile is not "img" then
display alert "A message" as critical
else
set thePath to POSIX path of item 1 of myImageFile
doIt(thePath)
end if
end tell
end open
Change the line: doIt(thePath) in the open handler into: my doIt(thePath).
The reason for the error, is that you try to use your handler from within the scope of finder, and finder, doesn't reckognize it as something of its own. Therefore you must add myin front of it, so the handler can be resolved.
How do I remove passwords from multiple PDF files using Applescript or by creating a Workflow in OS X?
My scenario is that I have multiple password protected PDF files in a folder. I know the passwords for all, which is same. I want to be able to run a Workflow on this folder so that all PDFs inside it are unlocked by the workflow.
OR run an Applescript shell code on all these files at once
I also preferably want to be able to create a way where putting / moving / pasting any PDF in the folder automatically unlocks it :)
Help appreciated !!
Update:
I have tried pdftk. The following code works awesome in Terminal, once pdftk is installed
pdftk secured.pdf input_pw foopass output unsecured.pdf
Now I want to be able to create a workflow that runs this command on selected files or on all the files in a folder
The AppleScript command to execute a shell script is do shell script...
So something like this:
do shell script "pdftk secured.pdf input_pw foopass output unsecured.pdf"
should work.
At this point I see 2 options:
write an AppleScript script that ask the user for the folder or get it from the Finder selection and then execute the command for each file in the folder;
write an Automator workflow that get the files from the folder using already available actions and then attach a new action that execute the AppleScript script.
For option 2 you can set an Automator workflow as in the following image.
Have you heard of "Folder Actions"? It's a way to attach an applescript to a folder so that whenever a new file is added to the folder the applescript is run. A quick google search turned up this which will give you directions on how to set it up. You can do more google searching if you still have questions.
Here's an applescript you can use with folder actions. I didn't test it but it should work (it's basic code). This will do its stuff on only pdf files. Other files you add to the folder will be left alone. NOTE: you have to put in your values for the first 4 variables of the script.
Good luck.
on adding folder items to theFolder after receiving theItems
-- enter your values here
set pdftkPosixPath to "/usr/bin/pdftk"
set pWord to "foopass"
set appendedName to "_unlocked" -- text to append to the file name
set shouldTrash to true -- true or false, move the locked file to the trash after unlocking?
set fContainer to theFolder as text
repeat with anItem in theItems
try
tell application "System Events"
set fName to name of anItem
set fExt to name extension of anItem
end tell
if fExt is "pdf" and fName does not contain appendedName then
set baseName to (text 1 thru -5 of fName) & appendedName & ".pdf"
set newPath to fContainer & baseName
do shell script (quoted form of pdftkPosixPath & space & quoted form of POSIX path of anItem & " input_pw " & quoted form of pWord & " output " & quoted form of POSIX path of newPath)
if shouldTrash then
tell application "Finder" to move anItem to trash
end if
end if
end try
end repeat
end adding folder items to
EDIT: here's how you can ask for a password. Note that if you want to see the text then remove "with hidden answer".
display dialog "Enter a password:" default answer "" with icon note with hidden answer
set theAnswer to text returned of the result
if theAnswer is not "" then set pWord to theAnswer