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.
Related
I have a problem with a workflow that stopped working when I upgraded to a MacBook Pro M1.
This workflow was used as a Quick Action in Finder files. First I tried running directly from Automator, and to my surprise it works, then tried again as Quick Action and doesn't work. I have been searching the Internet for more than a month and trying with different approaches but can't make it work again from Quick Action.
I have extracted just the code with the problem for easier explanation. The intention is to get the size of a movie file. The following code works inside Automator but issues an error when run from a Quick Action: cannot find the file.
tell application "Finder"
set existingMovies to (every file of theFolder) as alias list
end tell
repeat with movie in existingMovies
--repeat with movie in input
set theMovie to the (quoted form of POSIX path of movie) as string
set movieSize to do shell script "/usr/bin/mdls -name kMDItemDurationSeconds -raw -nullMarker 0 " & theMovie
end repeat
Then I tried working with input parameter of the workflow (in this case a list of files since this workflow uses "Workflow receives current: movie files"). With this approach it works both from Automator and as Finder Action
repeat with movie in input
set theMovie to the (quoted form of POSIX path of movie) as string
set movieSize to do shell script "/usr/bin/mdls -name kMDItemDurationSeconds -raw -nullMarker 0 " & theMovie
end repeat
In all scenarios I am using the same folder.
It seems that the list of files specified by input is somehow different from the list of files I create from a specified folder (existingMovies).
Any idea why this happens?
I can't replicate your problem on my machine, so I can't say for sure what's going wrong. But here's a 'best practices' reworking of your script. See if it solves your problem.
on run {input, parameters}
-- always use System Events, not the Finder, where possible
tell application "System Events"
repeat with thisFolder in input
set existingMovies to (every file of thisFolder)
repeat with movie in existingMovies
-- dereference properties with 'get'
set theMovie to the (quoted form of (get POSIX path of movie))
set movieSize to do shell script "/usr/bin/mdls -name kMDItemDurationSeconds -raw -nullMarker 0 " & theMovie
end repeat
end repeat
end tell
return input
end run
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 adapt an applescript I have to exclude several file types. I think I need something like -x ".fla" but I can't seem to get the syntax working.
on run {input, parameters}
if input is {} then -- no dropped items
tell application "Finder" to set input to selection as alias list
end if
repeat with x in input
set _path to POSIX path of x
tell application "Finder"
if kind of x is "Folder" then tell me to zipFolder(_path)
end tell
end repeat
end run
on zipFolder(theFolderPath)
do shell script "tDir=" & (quoted form of theFolderPath) & "; cd \"$tDir\"; aZip=\"../$(basename \"$tDir\").zip\"; if [ -e \"$aZip\" ]; then rm \"$aZip\"; fi; zip -r \"$aZip\" ./"
end zipFolder
Thank you for any help you can give
Add the exclude option before the last double quote:
./ -x *.fla"
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
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