Arty14
Arty14

Reputation: 51

Execute a shell command on a file selected in the Finder

I'm a very novice and infrequent applescript experimenter. I've tried for several hours now to learn the individual applescript commands for the following task, but I always run into errors. Perhaps someone much more adept at applescript will find this task easy and quick, and for that I would be very grateful. Here is the task:

I want to be able to manually select a document or file within the finder and then execute the following unix command on that file. I would then store the script under Finder's "Services" menu. The unix command is:

srm -rfv -m path/filename

In my attempts, I assumed that a script that would open Terminal and execute the command would be the way to go, but just couldn't get anything to work. Thank you in advance to any good programmers who can whip out such a script for me.

Upvotes: 5

Views: 2663

Answers (3)

mklement0
mklement0

Reputation: 437111

Update: I missed that the OP wants to create an OS X Service that integrates with Finder. ShooTerKo's answer shows how to do that (and his solution doesn't even require use of AppleScript).

The only potentially interesting thing about this answer is that it demonstrates AppleScript commands to open a file-selection dialog, get the chosen file's POSIX path and run a shell command with it, with some general background information about executing shell commands with do shell script.


Try the following:

# Let the user choose a file via an interactive dialog.
set fileChosen to choose file

# Get the file's POSIX path.
set filePath to POSIX path of fileChosen

# Use the path to synthesize a shell command and execute it.
do shell script "echo srm -rfv -m " & quoted form of filePath

Note:

  • There's no explicit error handling; if you don't want the script to just fail, you'll have to add error handling (try ... on error ... end try) to handle the case of the user canceling the file selection and the shell command failing (unlikely in this case).

  • The shell command has echo prepended to it in order to perform a dry run (see its output in Script Editor's Result pane); remove it to perform the actual deletion.

  • quoted form of is important for ensuring that a string value is included as-is in a shell command (no risk of expansion (interpretation) by the shell).

  • do shell script will NOT open a Terminal window - it will simply run the shell command hidden and return its stdout output, which is usually what you want. If the shell command signals failure via a non-zero exit code, an error is raised.

Upvotes: 1

ShooTerKo
ShooTerKo

Reputation: 2282

My tip: Create such services using Automator!

  1. Create a new Service in Automator
  2. Choose "File & Folder" as Input and "Finder"
  3. Add "Run shell script"
  4. Choose "as arguments" as input
  5. Change echo "$f" to your command srm -rfv -m "$f"
  6. Save it as "Safe delete"

From now on, if you select a file inside Finder you will find the option "Safe delete" in the context menu.

Enjoy, Michael / Hamburg

Upvotes: 7

pbell
pbell

Reputation: 3095

Craig's comment is pertinent, but I am just focus on the script itself, not on the shell command. the script bellow must be saved as Application and each time you drop 1 or more file on its icon, the shell script command will be executed for each file :

on open myFiles
repeat with aFile in myFiles -- repeat loop in case you drop more than 1 file on the icon script
try
do shell script "srm -rfv -m " & (quoted form of POSIX path of aFile)
end try
end repeat
end open

Still make sure that in your shell command 'srm -rfv', the 'v' is necessary because this script will not display any thing ! I don't think so. also I did not display error during remove. what do you want to do with error (like write protect, ...) ?

Upvotes: 1

Related Questions