I have tried to make an AppleScript that is supposed to read the current directory from Finder and run a shell command on it. If I navigate to the desired folder in Finder and run the script from AppleScript Editor it works, but when I saved the script and dragged it to the Finder toolbar, currentDir is set to the folder of the script file (my user directory). Here is my script:
tell application "Finder"
set currentDir to POSIX path of ((container of (path to me)) as text)
end tell
tell application "Terminal"
do script "cd " & currentDir
do script "<operation goes here>"
end tell
How can I get the directory active when I use the toolbar shortcut? Second, is there a way to run the shell command in the background, without opening (showing) the Terminal window?
Here are two solutions:
1- If the current folder is the target of the front Finder window :
tell application "Finder" to set currentDir to (target of front Finder window) as text
do shell script "cd " & (quoted form of POSIX path of currentDir) & "; <operation goes here>"
--
2 - If the current folder is the selected folder, it will be different in ( list view or coverflow) with respect to the target of the window, because you can select a sufolder in the window (the target of the window will not change) :
tell application "Finder"
set sel to item 1 of (get selection)
if class of sel is folder then
set currentDir to sel as text
else
set currentDir to (container of sel) as text
end if
end tell
do shell script "cd " & (quoted form of POSIX path of currentDir) & "; <operation goes here>"