Relative Posix Paths

I’m writing a script that calls a custom shell command to do operations on the file that is dropped on the script. Right now I call Terminal with the full POSIX path to the command. I’d like to be able to send my script and custom shell command to others, so I’d like to pass Terminal a POSIX path that is relative to the location of the script. That way, the script will always find the shell command, as long as it is in the same location relative to the script. How can I get the POSIX path of the working directory of the script from within the script? I’ve tried several variations, like “path of me” but I haven’t figured it out yet.

Alternately, if someone can give me a more reliable way of pointing the applescript at the shell command, I’ll try that.

Here’s some sample code:


on open these_items
	repeat with this_item in these_items
		
		try
			activate
			tell application "Terminal"
				activate
				do script with command "/full/path/to/shell-command " & quoted form of POSIX path of this_item
			end tell
		on error error_message
			beep
			display dialog error_message buttons {"OK"} default button 1
		end try
		
		
	end repeat
end open

This works when I drag and drop a file or folder onto the Applet…

on open these_items
	repeat with this_item in these_items
		try
			tell application "Terminal"
				activate
				do script "/bin/ls " & quoted form of POSIX path of this_item
			end tell
		on error error_message
			beep
			display dialog error_message buttons {"OK"} default button 1
		end try
	end repeat
end open

You had an extra “activate” command in your script and “do script with command” has been depricated to “do script”

Thanks for the corrections. However, my original question stands:

How do I change “/full/path/to/shell-command” to a path that is relative to the script?

Is there a better way to point to shell commands when they have no standard location? /bin/ls is not a good example, in this case.

I don’t fully understand how Applescript points to applications (ex, when you tell it to open BBEdit). It seems to be able to point to them no matter where they are. I want to do the same thing for shell commands.

Maybe the only answer is to put the shell command in a standard location, or to add it to $PATH. In my case, that solution is less attractive because I want to distribute the shell command with the applescript.

I’m not sure if this will work for you, but could you incorporate the “which” command into your script? That would give you the path to the shell-command, then you could set it as a variable.

set myShCommand to (do shell script "which pwd")
display dialog result & myShCommand

another stab in the dark

A good stab, but still not quite there. “which” will only give you the path if the shell command is already in your $PATH. I come from the UNIX world, so I’m fairly sure I’ve hashed out the solutions in that domain. I don’t know AppleScript as well.

This is a possible AppleScript solution:
Somehow get the string representation of the POSIX path to the folder containing the running script (“me”?). Then, I will just place my shell command within that same folder, or an enclosing folder, call the command based on the path obtained from the script, and they will work together as long as the folder is moved as a unit.

With an added modification, it could be very portable:
Somehow make an .app out of the folder containing my applescript and shell command. When someone double-clicks on the .app icon or drags a file onto the .app icon, it would run the applescript as though the double-click/drag had been done to it. This just adds more enforced portability, and less visibility of supporting shell command(s). I have NO idea if this is how you’re supposed to use .app’s.

Just a quick question. Are you creating new shell commands, or are you writing shell scripts? I’m a little unclear as to why a unix command would not be in the users $path. Please bear with me, I’m a novice unix nut, far from being an expert…

I haven’t really tried to follow this thread because it makes my head hurt but maybe this will be useful. :stuck_out_tongue:

set ptm to (path to me)
tell application "Finder" to set myContainer to container of ptm as text
display dialog POSIX path of myContainer

Great question. I am writing my own shell commands (actually, it’s a command-line compiled application written in C, but I’ve called it a “shell command” to avoid confusing it with the applescript “script” or “application.”)

Thanks Rob, that’s a clever piece of code.

sjschultze, would it be possible to create a ./config file that would install your custom commands in an appropriate directory, be it /bin or whatever? That way they would always be where you expect them to be. Or would your users know how to handle that? If not, it may be possible to create an AppleScript with a minimal interface that would assist your users with the installation of your commands into an already established executable $path, such as the /bin dir.

Just out of curiosity, what do your custom commands do, if ya don’t mind me asking – no offense taken if you don’t want to disclose that info.

Rob: Thanks for the code. It works perfectly.

Gregg: The command is actually an open-source command-line sound conversion utility, written originally to compile on Linux/Unix and Windows. I compiled it in OS X (with no code changes!) and wanted to build a wrapper around it so that someone could drop an audio file onto it and have it automatically converted. I changed the icon, dragged it into my dock, and now I have a cute little icon in my dock that I can drag an audio file onto any time I want.

I’m still curious about the .app folder structure and whether it would be possible to put an applescript in a .app folder, have it called when the .app is double-clicked, and include supporting scripts/files/whatever in the same .app directory.