Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use AppleScript to create drag and drop icons for shell scripts UNIX
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.
    •    
  • Currently 2.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[24,140 views]  

Use AppleScript to create drag and drop icons for shell scripts | 4 comments | Create New Account
Click here to return to the 'Use AppleScript to create drag and drop icons for shell scripts' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use AppleScript to create drag and drop icons for shell scripts
Authored by: ChiperSoft on Dec 25, '03 02:28:02AM
Since this script doesn't actually display anything (it basically launches and then quits), it can be handy to disable it's icon in the dock. The easiest way to do this is to save the script as Application Bundle. Control-click the script application and choose Show Package Contents from the menu. Add the following lines to the Info.plist file:
	<key>NSUIElement</key>
	<string>1</string>
The script will now launch without a face.

Note that in order for the window to close when the command completes, you need to change the default Window Settings to close the window when the shell exits.

[ Reply to This | # ]

Use AppleScript to create drag and drop icons for shell scripts
Authored by: vondrix on Dec 25, '03 08:00:23AM
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.

You can also say:

set path to POSIX path of i
and use
quoted form of path



[ Reply to This | # ]
Use AppleScript to create drag and drop icons for shell scripts
Authored by: ClarkGoble on Dec 25, '03 01:09:15PM
Just one other tip. If you go to Get Info for a particular file you can associate all files of that kind with any application including these drag and drop Applescripts. This is a great technique for associating files with programs you installed with Fink. You can also do cool things like automatically convert postscript files to PDFs and open them with Preview. (That hint was one sent in a few months back)
on open input_file
	
	do shell script "export PATH=\"$PATH\":/sw/bin;ps2pdf " & POSIX path of input_file & " /tmp/.psview.tmp.pdf"
	do shell script "open /tmp/.psview.tmp.pdf"
	
end open


[ Reply to This | # ]
Use AppleScript to create drag and drop icons for shell scripts
Authored by: wgscott on Dec 26, '03 11:59:58AM

Another option, which has the advantage of not opening a terminal window (unless you need it and code it specifically) is platypus, which is free and reasonably easy to use.

http://sveinbjorn.vefsyn.is/platypus



[ Reply to This | # ]