THE WHAT AND WHY
From inside Finder I want to be able to make hidden any files/folders, regardless if sudo is required or not, by a simple right click.
STEPS TAKEN ALREADY:
After a bunch of experimenting I settled on an Automator service running an AppleScript
on run {input, parameters}
set filehide1 to {}
repeat with filehide2 in input
set end of filehide1 to POSIX path of filehide2
end repeat
do shell script "chflags hidden " & quote & filehide1 & quote with administrator privileges
end run
ISSUE:
So the script works peachy BUT in it's present form only when one item at a time is selected. How do I tweak Automator / AppleScript to work regardless if its 1 file or 500?
You can't pass an AppleScript list to the shell. You have to flatten the list and separate the file paths by space characters. This can be done with text item delimiters.
The script escapes each file path in the repeat loop so the quotes are not needed in the do shell script line.
on run {input, parameters}
set filehide1 to {}
repeat with filehide2 in input
set end of filehide1 to quoted form of POSIX path of filehide2
end repeat
set {TID, text item delimiters} to {text item delimiters, space}
set fileList to filehide1 as text
set text item delimiters to TID
do shell script "chflags hidden " & fileList with administrator privileges
end run
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
I am trying to make a script that executes a runnable Jar file in the form of an applescript application. I'm not very experienced in applescript, so I mostly went off of online snippets. The first problem I ran into was that the directory automatically formats as using a ":" instead of a slash (ex. "usr:bin" instead of "usr/bin") so I found something that's supposed to replace the : with a /. This, however, produces an error every time.
I've found little on how to fix my error and the stuff I found was very specific to those cases and didn't make much sense to me.
This is my code:
tell application "Finder"
set current_path to container of (path to me) as alias
end tell
set AppleScript's text item delimiters to ":"
set currPath to text items of current_path
set AppleScript's text item delimiters to "/"
set current_path to currPath as string
set AppleScript's text item delimiters to {""}
currPath
tell application "Terminal"
activate
set currentTab to do script ("cd " & current_path)
set currentTab to do script ("java -jar Inoisulcnoc Pre-Alpha 2")
end tell
The error I get every time says:
error "Can’t get every text item of alias \"Macintosh HD:Users:knotsnappy:Desktop:Inoisulcnoc Pre-Alpha 2:\"." number -1728 from every text item of alias "Macintosh HD:Users:knotsnappy:Desktop:Inoisulcnoc Pre-Alpha 2:"
How should I be able to fix this?
You'll want to change the standard path into a POSIX path.
tell application "Finder"
set current_path to (POSIX path of (container of (path to me) as alias))
end tell
tell application "Terminal"
activate
set currentTab to do script ("cd " & current_path)
set currentTab to do script ("java -jar Inoisulcnoc Pre-Alpha 2")
end tell
The delimiters, etc. are unneeded once you have the correct syntax.
I'm trying to get my Applescript code to have administrator privileges. However, the only solution I find by googling is:
do shell script "command" user name "me" password "mypassword" with administrator privileges
I'm not running a shell command.. I'm using pure Applescript. My code I'm doing is:
on run {input, parameters} -- copy
repeat with aFile in input
tell application "Finder"
if name extension of aFile is "component" then
copy aFile to "/Library/Audio/Plug-ins/Components"
else if name extension of aFile is "vst" then
copy aFile to "/Library/Audio/Plug-ins/VST"
end if
end tell
end repeat
end run
Is there anyway to get admin privileges while using pure Applescript?
Your handler starts with on run {input, parameters} so I think we are talking about a execute applescript step inside an Automator workflow. At this point I think the Automator action is always executed inside the current user's context.
BUT: Of course you can use the do shell script inside the executed Applescript action and at that moment you can give the administrator privileges to that do shell call. I've rebuilt your handler in the following way:
on run {input, parameters} -- copy
-- collect all resulting cp statements in a list for later use
set cpCalls to {}
-- walk through the files
repeat with aFile in input
tell application "System Events"
-- get the file extension
set fileExt to name extension of aFile
-- get the posix path of the file
set posixFilePath to POSIX path of aFile
end tell
if fileExt is "component" then
-- adding the components cp statement to the end of the list
set end of cpCalls to "cp " & quoted form of posixFilePath & " /Library/Audio/Plug-ins/Components/"
else if fileExt is "vst" then
-- adding the vat cp statement to the end of the list
set end of cpCalls to "cp " & quoted form of posixFilePath & " /Library/Audio/Plug-ins/VST/"
end if
end repeat
-- check if there were files to copy
if cpCalls ≠ {} then
-- combine all cp statements with "; " between
set AppleScript's text item delimiters to "; "
set allCpCallsInOne to cpCalls as text
set AppleScript's text item delimiters to ""
-- execute all cp statements in one shell script call
do shell script allCpCallsInOne with administrator privileges
end if
end run
The action now asks for the administrator credentials but you can add user name "me" password "my password" if you like.
To avoid prompting for credentials for each cp I collect all calls in a list and execute them at once at the end of the handler.
Greetings, Michael / Hamburg
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
Im building an app for learning purposes of using applescript to send multiple commands during an action. Below is the code I'm messing with but I have stripped out the actions in between the "" and replaced them with numbers. Everything works fine in applescript but making this into an NSApplescript initwithsource: line has been a bother.
tell application "Terminal"
activate
set currentTab to do script "1"
do script "2" in currentTab
do script "3" in currentTab
do script "4" in currentTab
delay 0.5
tell application "Finder" to set visible of process "Terminal" to false
end tell
What is the best way to combine this applescript into a single line? Thanks!
"What is the best way to combine this applescript into a single line?"
Use AppleScript? :-D
First, in AppleScript Editor, open the Preferences window and click the option to Show Script menu in menu bar.
Then choose Open Scripts Folder from the script menu item up in the upper right of the screen.
Create a new AppleScript .scptd document with the following script:
tell application "AppleScript Editor"
set string_ to text of first document
-- make a list with each line of the script
set stringLines to paragraphs of string_
set originalDelims to AppleScript's text item delimiters
-- add newlines
set AppleScript's text item delimiters to "\\n"
-- now combine the items in the list using newlines
set stringNewlines to stringLines as string
set AppleScript's text item delimiters to "\""
set stringNewlines to text items of stringNewlines
set AppleScript's text item delimiters to "\\\""
set stringNewlines to stringNewlines as string
set AppleScript's text item delimiters to originalDelims
set stringNewlines to "#\"" & stringNewlines & "\""
set the clipboard to stringNewlines
end tell
(Note that this script isn't perfect: it works fine for simple scripts like the one you provided, but isn't able to convert itself).
Save that as a script in the Scripts folder you revealed earlier.
Then open your script document you want to convert and make it the front document in AppleScript Editor. Then invoke the conversion script by choosing it from the Script menu.
Given the script you provided, it should produce the following constant NSString:
#"tell application \"Terminal\"\n activate\n set currentTab to do script \"1\"\n do script \"2\" in currentTab\n do script \"3\" in currentTab\n do script \"4\" in currentTab\n delay 0.5\n tell application \"Finder\" to set visible of process \"Terminal\" to false\nend tell\n"