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
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
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
I want to be able to right click on a file or folder in Finder and select Services > Open Terminal to open a terminal window at that path.
In automator, I have a service that runs an AppleScript
tell application "Terminal"
do script "cd $filePath"
activate
end tell
I don't know how to pass the file path!
Bonus: How can I ensure that this works for both files and folders? If its a file, it may say that the file is not a directory.
Bonus: Where could I have found this answer myself? The documentation seems to be way to dense.
Thanks
Chet
Note the "Service receives selected ... " at top, which gives result to applescript.
This will open folders and containers of files, but won't be redundant.
on run {input, parameters}
set didThese to {}
repeat with f in input
set f to (f as text)
if f ends with ":" then
if f is in didThese then --check to see if we did this already
--ignore
else
tell application "Terminal" to do script "cd " & quoted form of POSIX path of f
set didThese to (didThese & f) --load into memory for subsequent iterations of loop
end if
else
--first get containing folder, then use that
tell application "Finder" to set f to ((container of alias f) as alias as text)
if f is in didThese then
--ignore
else
tell application "Terminal" to do script "cd " & quoted form of POSIX path of f
set didThese to (didThese & f)
end if
end if
end repeat
activate application "Terminal"
end run
Gleaned from https://apple.stackexchange.com/questions/112731/automator-service-to-print-a-relative-path-of-selected-files-printing-everything
[edit:]
Incidentally, one thing left to decide is how to treat bundles, like .app files, which aren't really files. Using
set i to info for (alias f)
then
package folder of i
will enable the script to determine this, through an additional if/then branch. I personally don't mind it "cd"ing into bundles.
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
In Script editor i have written a command to boot the JAR.
do shell script "cd /Desktop/RunJar/; java -jar RunMyJar.jar"
and Saved as script file as a Application. When i click on script file jar get run.
My requirement
I would like get the name of file that has been dropped onto the script file and would like to pass the name of that dropped file as an argument to my jar.
I have implemented this in Windows but could not working similar on MAC O.S.
On Windows
I have placed a BAT file to Boot JAR along with the absolute file name, that has been dropped on the bat file on windows. #echo %* will give me the list of files that has been dropped onto Batch file.
#echo %*
#pause
java -jar RunMyJar.jar %*
Similar i would like to implement in MAC O.S.
Thanks.
I found the following example at: Ben's AppleScript Snippets:
on open of finderObjects -- "open" handler triggered by drag'n'drop launches
repeat with i in (finderObjects) -- in case multiple objects dropped on applet
displayName(i) -- show file/folder's info
if folder of (info for i) is true then -- process folder's contents too
tell application "Finder" to set temp to (entire contents of i)
repeat with j in (temp)
display dialog j as string -- example of doing something with each item
end repeat
end if
end repeat
end open
You can also easily modify my answer to a similar question:
on open of theFiles -- Executed when files are dropped on the script
set fileCount to (get count of items in theFiles)
repeat with thisFile from 1 to fileCount
set theFile to item thisFile of theFiles
set theFileAlias to theFile as alias
tell application "Finder"
set fileInfo to info for theFileAlias
set fileName to name of fileInfo
-- something to this effect, but now that you have the file name,
-- do what you will...
do shell script "cd /Desktop/RunJar/; java -jar " & fileName
end tell
end repeat
end open
To add to Paul's answer
on open of finderObjects
repeat with currFile in finderObjects
-- ok, we have our current item. But it's an "alias": a Mac OS path
-- not a POSIX path
set unixPath to POSIX path of currFile
set base to do shell script "dirname " & unixPat
set fname to do shell script "basename " & unixPath
-- you could ask the Finder to give this to you too -- I'll use this way because
-- I'm guessing it's more familiar to you
do shell script "cd '" & base & "';" & " java -jar " & fname
end repeat
end open