4

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
Arty14
  • 41
  • 3
  • Using the v flag directs the srm to be verbose. Do you have a plan for returned text? – Craig Smith Oct 15 '15 at 20:53
  • To summarize: You're looking to create an OS X Service that integrates with Finder: a shell command that safely deletes files should be applied to the currently selected items in Finder. You can create such a Service in Automator and don't even need to involve AppleScript, as [ShooTerKo's](http://stackoverflow.com/a/33172126/45375) helpful answer demonstrates. – mklement0 Oct 17 '15 at 13:23

3 Answers3

7

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

ShooTerKo
  • 2,242
  • 1
  • 13
  • 18
  • Well done; sounds like that's exactly what the OP is looking for. – mklement0 Oct 17 '15 at 13:04
  • Michael--Your response is very helpful. I am grateful of the simplicity and for your suggestion that I might save the command in the Finder context menu rather than the services menu. I'm grateful for your help...thanks! – Arty14 Nov 01 '15 at 04:56
1

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, ...) ?

pbell
  • 2,965
  • 1
  • 16
  • 13
1

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.

Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Good answer - have my vote. If OP wants to make something a little more flexible, have a look at @jweaks excellent answer here http://stackoverflow.com/a/33088853/2836621 which allows the script to run either by starting it and selecting a file, or by selecting a bunch of files in the Finder and dropping them onto the script. – Mark Setchell Oct 17 '15 at 09:46
  • Thanks, @MarkSetchell. The linked script is interesting, but on re-reading the question I noticed what the OP really wants is an OSX Service that is directly integrated with Finder, which is what ShooTerKo's answer describes, so I've pointed my answer there. – mklement0 Oct 17 '15 at 13:06
  • Thank you all for great input. I'm gonna try each of these suggestions one at a time to start to understand all of the relative weaknesses and benefits. – Arty14 Nov 01 '15 at 04:52