Use AppleScript to create drag and drop icons for shell scripts

Dec 24, '03 12:52:00PM

Contributed by: Anonymous

You can use AppleScript to create drag and drop icons for shell scripts or X11 applications. Here's how you would create a drag and drop icon for Emacs in Terminal.app:

  1. Launch Script Editor (Applications -> AppleScript -> Script Editor
  2. Copy and paste this text into a new Script Editor window:
    
    set filecount to 0
    
    on open filelist
     repeat with i in filelist
      set filecount to 1
      tell application "Terminal"
       set filename to do shell script ¬
        "perl -e \"print quotemeta ('" & POSIX path of i & "');\""
       do script "emacs " & filename & "; exit"
      end tell
     end repeat
    end open
    
    if filecount < 1 then
     tell application "Terminal"
      do script "emacs; exit"
     end tell
    end if
    
Save as an application, and give it the name Emacs.app or something similar. Dragging and dropping a file onto Emacs.app will open the file in Emacs in a new Terminal window. Double-clicking will open Emacs in a new Terminal window.

This line:


set filename to do shell script ¬
"perl -e \"print quotemeta ('" & POSIX path of i & "');\""
is in there to deal with filenames which have spaces or special characters. POSIX path is the UNIX path of a file; the perl -e replaces /Users/foo/file with spaces in its name with /Users/foo/file\ with\ spaces\ in\ its\ name.

Modify the do script lines if you want to run a different UNIX command:

tell application "Terminal"
 do script "foo"
end tell
The above will run a script in a Terminal window. But sometimes, you don't want to open a Terminal window. In those cases, use this AppleScript code:

do shell script "foo"
Note that if you've modified your $PATH in your ~/.profile, do shell script won't see your new $PATH. In order to run something that's not in /usr/bin, /bin, /usr/sbin, or /sbin, you'll need to do something like this:

do shell script "source ~/.profile; foo"
That's what to do if you use bash (like me). I'm not sure what you would do if you use tcsh and your $PATH has been edited in your ~/.cshrc.

Here's how you can create an AppleScript to launch X11 applications, using xemacs as an example:
  1. Launch Script Editor (Applications -> AppleScript -> Script Editor)
  2. Copy and paste this text into a new Script Editor window:
    
    set filecount to 0
    
    run application "X11"
    
    on open filelist
     repeat with i in filelist
      set filecount to 1
      set filename to do shell script ¬
       "perl -e \"print quotemeta ('" & POSIX path of i & "');\""
      do shell script "source ~/.profile; xemacs -display :0.0 " & filename & " &"
     end repeat
    end open
    
    if filecount < 1 then
     do shell script "source ~/.profile; xemacs -display :0.0"
    end if
    
    This script assumes that the $PATH variable has been altered in ~/.profile to include the directory which xemacs is in.

    Comments (4)


    Mac OS X Hints
    http://hints.macworld.com/article.php?story=20031027142625782