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

Another way to create Finder-clickable shell scripts UNIX
Want to make a double clickable shell script? Don't want to use some convoluted app that works most of the time? Don't have time to find one that you like? Make a shell script, then try this instead:
SetFile -t APPL someScript.sh 
Ta da! Suddenly the script is double-clickable in the Finder! Remember that it will always be run as if it were in the root of the disk, so always use absolute paths! You'll need the Dev Tools installed to do this, as Setfile is in /Developer/Tools. You can make a custom icon by pasting one into the Get Info window for the script in Finder.

For those of you who care, this works by setting the Type code (the resource that the Finder looks at to check if something is an application) to APPL, which indicates that it's an application. You can also set the creator code if you like with the -c option, but it's useless. Enjoy!

[robg adds: We showed another easy way of doing this in an earlier hint...]
    •    
  • Currently 2.71 / 5
  You rated: 2 / 5 (7 votes cast)
 
[41,912 views]  

Another way to create Finder-clickable shell scripts | 30 comments | Create New Account
Click here to return to the 'Another way to create Finder-clickable shell scripts' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Another way to create Finder-clickable shell scripts
Authored by: gvitale on Jul 30, '03 10:55:12AM

Mmm... I get a -39 type error; any idea?



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: foobar104 on Jul 30, '03 11:19:18AM

There's a better way.

Let's say you have a shell script called foo.sh. Do this:

mkdir -p foo.app/Contents/MacOS
mv foo.sh foo.app/Contents/MacOS/foo
chmod +x foo.app/Contents/MacOS/foo

Done. Double-click foo.app (which the Finder shows you as simply "foo") and your shell script runs. No developer tools required, and it doesn't depend on fragile metadata like this hint does.



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: MacTed on Jul 30, '03 11:20:53PM

I tried this one, cause I like it better than the previous hint -- I can add a nice icon and other frills! -- but this failed for me...

The Finder decided that my foo.app was a Classic App, but didn't bring up the Classic Environment. My script is woefully simple -- it simply calls a Terminal-based executable -- which now launched in the background, with no user interaction...

So I'll stick to the following, as in the older hint, for now -- just change your

foo.sh

to

foo.command

and presto -- auto-launch of the Terminal, and auto-run of the script therein, complete with user interaction.

---
Ted Thibodeau Jr -- Technical Evangelist -- OpenLink Software

[ Reply to This | # ]

Another way to create Finder-clickable shell scripts
Authored by: _merlin on Jul 31, '03 03:42:01AM

You also have to create a file foo.app/Contents/Info.plist containing keys identifying the name of the executable. In this exampe, it would contain:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>CFBundleExecutable</key>
<string>foo</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.3.0</string>
</dict>
</plist>



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: foobar104 on Jul 31, '03 09:55:27AM

Nope. You don't actually have to do this.

When you double-click on foo.app, the Finder looks for a file foo.app/Contents/Info.plist. Not finding it, it looks for an executable file with the same name as the base name of the application package: foo.app/Contents/MacOS/foo. It then exec's it.

The only trick is that you have to be sure the file is executable.



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: peterhil on Jul 31, '03 04:49:14AM

Neat hint!

But to make it work, I had to make an Info.plist file in the bundle, which must contain the usual xml headers plus the following key at minimum:

	<key>CFBundleExecutable</key>
	<string>foo</string>

Apple's developer documentation about keys used in the bundles instruct not to use a file name extension for the actual program -- and after dropping the extension the shell script shows as an application inside the bundle too.

I have been looking for a way to make a "droplet" from shell script, so I could drop files into the shell script which would get the dropped files as arguments.

Editing the CFBundleDocumentTypes key in Info.plist didn't seem to achieve this. Does anyone know how this could be done?

This might be a great way to package Python programs too!



[ Reply to This | # ]
AppleScript droplets
Authored by: saint.duo on Jul 31, '03 11:34:43AM
I use applescript to accomplish this. Something along the lines of:
on open myList
   repeat for each myItem
      do shell script "PathToMyShellScript" & " " & myItem
   end repeat
end open
The code may be a little off, as I'm doing this for memory, but the idea works. I use it in my UnButton app, which you can get the source code for at http://www.freefallsoftware.com/products.html

---
--
duo

[ Reply to This | # ]

Another way to create Finder-clickable shell scripts
Authored by: gpturismo on Jul 30, '03 11:38:56AM

So you're saying if we set the creator to APPL it's always run as root? Is that a security risk? Say I am a limited user, I download setFile, and create a shellscript called Terminate.sh, and the base code is:

rm -r /

And set the creator to APPL, I can basically destroy the data on the root drive by double clicking the file I created?

That's bad news if it's true



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: SJT on Jul 30, '03 11:42:33AM

I'm pretty sure he means it's run from the root directory, not as root. That's why he mentions absolute paths...

e.g. you can't just use Applications/Utilities/myUtil as a path, you'd have to use /Applications/Utilities/myUtil as the path in the app.
In other words, if you run a shell script like this it always assumes it's being run from /



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: frankie1969 on Jul 30, '03 11:43:27AM

No, it's an unfortunate overloading of the word "root". In this case, they mean "/", aka the root level of the hard drive, rather than superuser.

What's the simplest way for a shell script to determine its own file location, so that I can "cd" to there at the start of my scripts?



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: vonleigh on Jul 30, '03 11:59:10AM

You don't need to know, just do a cd with an absolute path.



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: trekan on Jul 31, '03 07:37:45AM

The key point is that the path must be absolute. /Users/username/Desktop instead of Desktop and so on.

cd won't help you in a shell script. You must still use absolute paths in the rest of the script. What can save you though is using variables.

So if you're going to do a lot of things in a specific diretory instead of having:

ls /Users/username/sometempdir >> /var/logs/savedfiles.txt
rm /Users/username/sometempdir/*

You can do it like this:

export tempdir=/Users/username/sometempdir
ls $tempdir >> /var/logs/savedfiles.txt
rm $tempdir/*

Remember to include

#!/bin/sh

in the top of the shell script to tell that it should be executed by Bash. I'm not sure other shells support the export command.



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: trekan on Jul 31, '03 07:47:10AM
Oh yeah... if you want to use the script in my example, I would recommend:

#!/bin/sh
export tempdir=/Users/username/sometempdir
export date=$(date +'%Y%m%d-%H%M') ls $tempdir >> /var/logs/savedfiles.txt
tar czf /Users/username/$date.tgz $tempdir rm $tempdir/*

this script creates a tar-gzipped file with the date as the name in the home directory of the user username. It appends the list of files to the file /var/logs/savedfiles.txt and removes the files from the original directory



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: GaelicWizard on Aug 25, '03 12:45:36AM

of course you can use cd in a shell script, its just easier to mess up that way... it is a shell after all! :-D


---
Pell



[ Reply to This | # ]
How a shell script can find where it is located
Authored by: al451 on Feb 04, '04 03:37:33PM
The output of dirname $0 will tell you the directory in which the shell script is located. If your shell script wants to cd to the directory that contains the script itself, it should say
        cd `dirname $0`


[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: gpturismo on Jul 30, '03 12:24:29PM

My fault. I misread run in root as run as root. My Bad



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: chris_on_hints on Jul 30, '03 01:57:14PM

Another option:

For scripts that print output or require you to respond to them there is a neat solution:

Go to the terminal, set up the window settings (colour, position, size etc...) and go to File > Save and put the file where you want. It saves the settings as a ".term" file.

When you double click the .term file, it opens a new terminal window with the settings you chose. Edit the file in your favourite app (TextEdit is fine) and find the line:

<key>ExecutionString</key>
<string></string>

Insert the command (full path is probably safest) in between <string> and </string> and save the file.

This is great for putting a group of commands in (separated by semi colons) and for making the terminal window unique to that command!!



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: rossr on Jul 30, '03 07:49:32PM

There is a freeware program called Platypus that makes a shell script into a Finder-clickable app. The program also has options for using Perl, Python, Ruby and AppleScript (why would you need a program that makes an AppleScript executable? :\ )

Platypus

Launch the program, choose your options, and create an applet. Easy.

I use this application to run a shell script that compiles statistics of my Jedi Knight II game server. :)



[ Reply to This | # ]
Drag-and-Drop Input
Authored by: felistigre on Jul 30, '03 08:53:14PM

What about file input? It would be great if I could drop some text files on a such an application, but Platypus doesn't support it.



[ Reply to This | # ]
Drag-and-Drop Input
Authored by: peterhil on Jul 31, '03 08:03:15AM
I was looking for Drag and Drop input too and found ScriptGUI, which passes the files dragged as arguments to the script if you save your app as scriptlet.

[ Reply to This | # ]
Drag-and-Drop Input
Authored by: pmccann on Jul 31, '03 08:40:53PM

Apologies if this has been mentioned above, but I didn't see it: DropScript (search on versiontracker) does what the hint claims to do, but reliably, freely (in whatever sense you wish), and allows files to be dragged onto the resulting icon (unlike platypus, at least for now). It's been available since 2001.

Cheers,
Paul



[ Reply to This | # ]
Drag-and-Drop Input
Authored by: tinb on Aug 03, '03 03:30:24PM

<shameless plug>
Please have a look at my TurboTool (www.turbotool.de) which features D&D input very well and was created with those applications in mind
</shameless plug>



[ Reply to This | # ]
Platypus
Authored by: sveinbjorn on Jul 30, '03 07:49:32PM
I released an open-source freeware app that creates application bundles from scripts a while ago. It's called Platypus.

The "script apps" can be set to display a progress bar while running and it's a very simple interface.

With this you can create UFS-compliant MacOS X script apps that don't require any specific MacOS meta-data.

[ Reply to This | # ]
Platypus
Authored by: rossr on Jul 30, '03 07:51:56PM

Wow. Talk about being on the same wavelength! We posted simiar messages within seconds of each other!



[ Reply to This | # ]
Platypus
Authored by: amacaulay on Jul 30, '03 08:20:55PM

Spooky - I grabbed Platypus yesterday but haven't looked at it yet.



[ Reply to This | # ]
Platypus
Authored by: gvitale on Jul 31, '03 07:50:16AM

sveinbjorn thank you so much: your appl works, where the hint doesn't



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: gvitale on Jul 31, '03 08:29:15AM

Has anybody been successful with this hint? I get a -39 type error ("eofErr=-39, /*End of file*/") when I double click the .sh file modified with APPL Type code. The script is otherwise functional.
I tried to add a EOF line at the end of the file, but it makes no difference...
Can someone help?



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: weezer51402 on Aug 24, '03 08:58:07PM

In Panther 10.3 shell scripts/unix executables become clickable



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: magir on Jan 02, '06 05:48:47AM

Under OSX 10.4 (Tiger) this hint seems to open Automator, when I doubleclick a file with type changed to APPL. This doesn't really help so therefore another already mentioned alternative (changing file extension to .command) seems to work better.



[ Reply to This | # ]
Another way to create Finder-clickable shell scripts
Authored by: bubu on Dec 05, '09 01:45:45AM

actually, it is better to change the Creator :

setFile -c trmx myScript

probably it is better to set the x flag (chmod +x myScript)

Now, does anyone know how to get the directory (in the script) where the file is located ?



[ Reply to This | # ]