Writing current file path in comment of file in Automator - bash

I'm trying to write the current file path in the comment text box; the one you see when you right-click and Get Info. I'm pretty close, because I got it to write a file path to all the files inside a folder, but all the file paths are the same for some reason.
For example, when I right click a file after I run the script and see the comment, it might be "'/Users/Admin/Desktop/automator test/folder/spikyBall#2x copy 4.png'" and it will be that for all the files.
My Shell Script variable is defined as:
bashFilePath=$(osascript -e 'tell application "Finder" to set filePath to quoted form of posix path of (item 1 of (get selection) as text)');
echo $bashFilePath;

If I am interpreting your question correctly, try this:
echo $1
In shell scripts, $1 is the first argument ($2, $3, etc. also work). So the above just echoes the first argument... which with your automator program is the path of the file.

I ended up using pure Apple script for this. Select any files and folders that you want and run the script. It can also be made into a files or folders Service in Automator.
tell application "Finder"
activate
set fileList to selection
if (count result) is 0 then
try
get (target of front Finder window) as alias
on error
choose folder with prompt "Set comments of files in this folder:"
end try
try
set theFolder to result
set fileList to every file of folder (result) as alias list
end try
end if
display dialog "How would you like to set comments?" buttons {"Overwrite", "Cancel"} default button 2 with title "Set Spotlight Comments current to file path"
set userInput to the result
set itemNum to 1
if (button returned of userInput) is "Overwrite" then
if (class of first item of fileList) is alias then
set comment of every file of folder theFolder to POSIX path of (item itemNum of (get selection) as text)
set itemNum to itemNum + 1
else
repeat with thisFile in fileList
set comment of thisFile to POSIX path of (item itemNum of (get selection) as text)
set itemNum to itemNum + 1
end repeat
end if
end if
end tell

Related

How to run Applescript Photoshop Automate Batch of an Action

I am trying to make an Applescript that will launch Photoshop and call the Automate Batch function to run a specific Action. I have zero experience doing this and have only gotten snippets of code from my searching. I was wondering if someone could help me on this.. in particular if it is at all possible to pass a source folder to the batch call and then how to make the Batch call.
In particular, I am having issues trying to figure out how to:
Pass the source folder into the Batch Options
Call a specific Action in the Batch_Options from my Photoshop
Run the batch call with these options
I've updated with the latest code that is partially there...
tell application "Finder"
set sourceFolder to "Macintosh HD:Users:Joe:Desktop:Temp:" as alias
set folderList to every item of folder sourceFolder as alias list
do shell script "echo File Names are: " & folderList
end tell
tell application "Adobe Photoshop CC 2018"
set action_set_name to "Save as Photoshop PDF"
activate
try
set Batch_Options to {class:batch options, destination:save and close, error file:Error_File, file naming:{document name lower, extension lower}, macintosh compatible:true, override open:false, override save:true, suppress open:true, suppressprofile:true, unix compatible:true, windows compatible:true}
batch "Save" from files folderList from action_set_name with options Batch_Options
end try
end tell
output:
"File Names are: Macintosh HD:Users:Joe:Desktop:Temp:Creature01_CO_v003.psdMacintosh HD:Users:Joe:Desktop:Temp:SecretLaboratory.psd"
Photoshop opens, and then nothing happens...
The following seems to work in the Automator:
tell application "Finder"
set sourceFolder to "Macintosh HD:Users:Joe:Desktop:Temp:" as alias
set folderList to every item of folder sourceFolder as alias list
do shell script "echo File Names are: " & folderList
#Alias list needs to be converted to a string for it to work in Photoshop batch call
set FileNames to {}
repeat with i from 1 to the count of folderList
set current_file to item i of folderList
copy current_file as text to the end of FileNames
end repeat
#Setup error log
set err_log to (sourceFolder as string) & "Error_Log.txt"
if not (exists file err_log) then
make new file at sourceFolder with properties {name:"Error_Log.txt"}
end if
end tell
tell application "Adobe Photoshop CC 2018"
set action_set_name to "SetA"
activate
set Batch_Options to {class:batch options, destination:save and close, error file:err_log, file naming:{document name lower, extension lower}, macintosh compatible:true, override open:false, override save:true, suppress open:true, suppressprofile:true, unix compatible:true, windows compatible:true}
#First arg is the Action Name, second is the list of files, third is the Action Set Name
batch "ActionA" from files FileNames from "SetA" with options Batch_Options
end tell

AppleScript - Change all app icons in a directory

I have folders containing applications. I want to be able to select the folder with an apple script, and have the script go through each app file in that directory, changing the icons for me.
I want to have the icon set from an image stored in the script directory.
I'd really appreciate any help because I've been trying to make this work for a while. This is my progress so far:
property appcurrentCount : 0
on run
set theFolder to (choose folder with prompt "Select the start folder")
doSomethingWith(theFolder)
end run
on doSomethingWith(aFolder)
tell application "Finder"
set subApps to every file of aFolder
repeat with eachFolder in subApps
-- replace icon here somehow
end repeat
end tell
display dialog "Count is now " & appcurrentCount & "."
end doSomethingWith
The script bellow does now what you want. As explained before, your icon file must be type icns. I now add this filter directly in the choose file command.
The selected icon will now replace ALL icons already in each Contents/Resources folder of all applications in the selected folder.
The replacement will be done preserving the name of the icns already in place.
Warning : there is no 'undo' command. your old icons are overwritten !!
set Myicon to choose file with prompt "Select Icns to be copied in every Application of the folder" of type "com.apple.icns"
set MyFolder to choose folder with prompt "Select the folder with all applications to be changed"
set Source to POSIX path of Myicon -- convert path to unix form
tell application "Finder"
set MyApps to every item of MyFolder whose name extension is "app"
display dialog "count apps=" & count of MyApps
repeat with anAps in MyApps -- loop for each App
set IcnFolder to ((anAps as string) & ":Contents:Resources:") as alias
set MyIcns to (every item of IcnFolder whose name extension is "icns")
display dialog "count of icn in " & alaps & " = " & (count of MyIcns)
repeat with oneIcon in MyIcns -- loop for each icns
set Destination to POSIX path of (oneIcon as string)
try
do shell script "cp " & (quoted form of Source) & " " & (quoted form of Destination)
end try
end repeat -- loop for each icns
end repeat -- loop for each App
end tell

How to reveal default Safari downloads folder through Applescript?

I am new to AppleScript and would like to know how to reveal the default downloads folder in Safari through AppleScript.
--- WHAT I HAVE ALREADY TRIED ---
set filePath to do shell script "defaults read com.apple.Safari DownloadsPath"
do shell script "open " & quoted form of filePath
The above script to my understanding will set the variable "filePath" to Safari's default PList entry in preferences.
This works great as long as the location is NOT somewhere within the user home folder. If its within a user folder, the Log shows me a "~/" before the path with no reference to anything prior to the user home folder (relative path)
How do i accomplish my goal? is there a way to get the absolute path to the folder? Or perhaps an alternative method?
Based on #WilliamTFroggard's answer:
tell application "Finder"
set folderPath to do shell script "defaults read com.apple.Safari DownloadsPath"
if folderPath = "~" or folderPath starts with "~/" then ¬
set folderPath to text 1 thru -2 of POSIX path of (path to home folder) & rest of characters of folderPath
open POSIX file folderPath as alias
activate
end tell
The text element is used to strip the trailing / from the home folder (/Users/johndoe/).
The rest property returns every character following the ~ in folderPath (~/Downloads).
/Users/johndoe + /Downloads = /Users/johndoe/Downloads.
You can do this by Applescript GUI Scripting to make Applescript click the 'show downloads' option from the 'view' menu:
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari"
tell menu bar 1
tell menu bar item "view"
tell menu "View"
click menu item "Show downloads"
end tell
end tell
end tell
end tell
end tell
Here's a method using Python:
do shell script "python -c \"import os; print os.path.expanduser('`defaults read com.apple.Safari DownloadsPath`')\""
If you really want the long AppleScript method, try this:
set filePath to ""
set AppleScript's text item delimiters to "/"
set filePathWords to words of (do shell script "defaults read com.apple.Safari DownloadsPath")
if item 1 of filePathWords is "~" then
set item 1 of filePathWords to (POSIX path of (path to home folder as string))
set filePath to filePathWords as string
else
set filePath to "/" & filePathWords as string
end if
If that second "/" in the path bothers you (it sort of bothers me...), you can use this instead:
set filePath to ""
set AppleScript's text item delimiters to "/"
set filePathWords to words of (do shell script "defaults read com.apple.Safari DownloadsPath")
if item 1 of filePathWords is "~" then
set item 1 of filePathWords to (do shell script "cd ~ && pwd")
set filePath to filePathWords as string
else
set filePath to "/" & filePathWords as string
end if
I think what you want is this:
tell application "Finder"
activate
open ("/Users/yourname/Downloads" as POSIX file)
end tell
Just replace "yourname" with your name in Finder.

Remove multiple PDF passwords with workflow or applescript in a folder

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

Using Automator or Applescript or both to recursively print documents to PDF

I have a massive set of files (4000+) that are in an old Apple format (Appleworks). My employed needs them all updated to PDF. By opening the documents in Appleworks and using the system print dialogue, I can save them to PDF—this is ideal. I'm a complete nub with Applescript/Automator, however.
Using a Python script I was able to gather all the Appleworks files from my bosses computer and put them in a directory; each file is then in a subdirectory with a .txt file containing its original location (where, eventually, I will have to put them back).
I need the script to move recursively through this massive directory, getting every file that's neither a folder nor a .txt document, and save it to PDF in the same directory in which the original file was found. ie.
/Appleworks/Boss_File_1/
will contain
/Appleworks/Boss_File_1/Boss_file_1.cwk and
/Appleworks/Boss_File_1/path.txt
But must eventually also contain /Appleworks/Boss_File_1/Boss_File_1.pdf
I can get half way with either solution, but don't know how to make them work together. The Applescript I'm using looks like:
set appleworksFolder to choose folder
tell application "Finder"
set folderItems to (files of entire contents of appleworksFolder)
repeat with I from 1 to number of items in folderItems
set the_doc to item I of folderItems
if name of the_doc is not "path.txt" then
try
tell application "AppleWorks 6"
open the_doc
tell application "System Events"
tell process "Appleworks"
keystroke "p" using command down
click menu button "PDF" of window "Print"
click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
click button "Save" of window "Save"
end tell
end tell
end tell
end try
else
tell application "Finder"
delete the_doc
end tell
end if
end repeat
end tell`
This opens the print dialogue but never gets any further and I have no idea why. I realize this script also doesn't deal with putting the document back in its original folder, but in Applescript I could easily enough do this if I could get past the actual printing-to-PDF bit.
Meanwhile, in Automator, using this workflow:
Get Specified Finder Items
Get Folder Contents
Filter Finder Items (by kind and then by file extension is not .txt)
Open Finder Items (with Appleworks)
I then am stuck; using the actual Print Finder Items and choosing Adobe PDF seems to actually do nothing at all, and recording myself using the print to pdf process live is useless because I don't know how to get Automator to retain the path the file originated from and ensure it prints to it.
If anyone can help me put this together somehow, I'd be enormously grateful. Thanks.
Convert using Pages
If you have Pages (part of iWork), it can open .cwk files and save them as PDF: just replace your if block with this:
if (the_doc's name extension is not "txt") then
set newName to my makeNewFileName(the_doc, "pdf")
try
tell application "Pages"
open (the_doc as alias)
set thisDoc to front document
save thisDoc as "SLDocumentTypePDF" in newName
close thisDoc saving no
end tell
on error
display dialog "Error: cannot export " & (name of the_doc) & " to PDF."
end try
end if
(you will need this custom function makeNewFileName):
(* prepare new file name with extension ext *)
on makeNewFileName(finderItem, ext)
tell application "Finder"
set fname to finderItem's name
set thePath to (finderItem's container) as alias as text
return (thePath & (text 1 thru ((length of fname) - (length of (finderItem's name extension as text))) of fname) & ext)
end tell
end makeNewFileName
(complete working script)
GUI scripting
Alternatively, you could do GUI scripting upon AppleWorks as you attempted, but it has the disadvantage that you cannot programmatically specify where to save the PDF file.
This snippet works for me:
tell application "AppleWorks 6"
open the_doc
activate
tell application "System Events" to tell process "AppleWorks"
keystroke "p" using command down
delay 1 -- or longer, if it takes longer
click menu button "PDF" of window "Print"
click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
delay 1 -- or longer
click button "Save" of window "Save"
end tell
end tell
Unfortunately, AppleWorks doesn't seem to properly listen to AppleScript's close command, therefore you may need to close the file by also simulating the cmd+W keystrokes.
Try this:
set appleworksFolder to choose folder
set thePath to POSIX path of appleworksFolder as string
tell application "Finder"
set folderItems to files of appleworksFolder
repeat with aFile in folderItems
set {name:fileName, name extension:nameExtension} to aFile
set filePath to POSIX path of (aFile as alias) as string
if nameExtension is not "txt" then
set theLocation to POSIX path of (aFile as text)
set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
set destLocation to (thePath & baseName & ".pdf")
set theCommand to "/System/Library/Printers/Libraries/./convert -f \"" & filePath & "\"" & " -o " & "\"" & destLocation & "\"" & " -j \"application/pdf\""
do shell script theCommand
else
tell application "Finder" to delete aFile
end if
end repeat
end tell
I needed to do this today on Mountain Lion with a bunch of RTF receipts; here's how I did it:
#!/bin/bash
for file in *.rtf ; do
filename=$(basename "$file")
/usr/sbin/cupsfilter "$file" > "$filename.pdf"
done
Worked great; super easy. No Automator or AppleScript silliness.

Resources