Path to the folder the script is in?

I used


path to frontmost application --returns path to script while its running

That will return the path to the script no matter what volume it’s moved to. How do I parse the path to get only the string up to the script’s containing folder?

:?: Is there an easier way to get the script’s “path to containing folder” if the containing folder is moved to any volume :?:
SC

is easier, and always works too, but I’ve always had to resort to text manipulation kluges to back up one in the alias string. Note too, that in either form, the script application looks like a folder (with a colon following it).

You can do a number of things, the following two being pretty common. If you don’t mind using the finder, it can do it in one line. Otherwise you’ll have to do some delimiter manipulation to get the string you want. Both of the following should be equivalent…

--> Use the Finder...
tell application "Finder" to set thePath to (container of (path to me)) as string
--> Use tid's...
set fullPath to (path to me) as string
set AppleScript's text item delimiters to {":"}
set containerPath to ((text items 1 through -3 of fullPath) as string) & ":"
set AppleScript's text item delimiters to {""}

return containerPath

j

For the thread, “path to me” returns the path to the script when ran as an app, not it’s container. Point of interest, if you “run” the script from the Script Editor, “path to me” yeilds the path to the Script Editor.
TY - NS, Jobu

SC
“Its all about the syntax” :rolleyes:

These are the routines I use for getting containers, item names, and extensions. I like them because they work on files and folders, HFS paths and POSIX paths, strings, aliases, or file specifications, and don’t rely on any application while still being vanilla AppleScript:

set item_path to (choose file)
set {container_path, item_name} to my get_container_and_name(item_path)
set {item_name, item_extension} to my get_name_and_extension(item_name)
get {container_path, item_name, item_extension}
-->{"Macintosh HD:Users:jon:Desktop:", "test", "txt"}

set {container_path, item_name} to my get_container_and_name(POSIX path of item_path)
set {item_name, item_extension} to my get_name_and_extension(item_name)
get {container_path, item_name, item_extension}
-->{"/Users/jon/Desktop/", "test", "txt"}

on get_container_and_name(p)
	set p to p as Unicode text
	set d to {"/", ":"}'s item (((p contains ":") as integer) + 1)
	set {l, t} to {my string_to_list(p, d), ({{-1, -2}, {-2, -3}}'s item (((p ends with d) as integer) + 1))}
	return {my list_to_string(l's items 1 thru (get t's item 2), d) & d, l's item (get t's item 1)}
end get_container_and_name

on get_name_and_extension(n)
	set d to "."
	set l to my string_to_list(n, d)
	if (count l) > 1 then
		return {my list_to_string(l's items 1 thru -2, d), l's item -1}
	else
		return {n, ""}
	end if
end get_name_and_extension

on list_to_string(l, d)
	tell (a reference to my text item delimiters)
		set {o, contents} to {contents, d as Unicode text}
		set {l, contents} to {"" & l, o}
	end tell
	return l as Unicode text
end list_to_string

on string_to_list(s, d)
	tell (a reference to my text item delimiters)
		set {o, contents} to {contents, d as Unicode text}
		set {s, contents} to {s's text items, o}
	end tell
	return s
end string_to_list

Jon

Much appreciated and notes taken.
TY
SC

Hi, Jon. Just to note that boolean-to-integer coercions were only introduced with Panther. If you’re writing code for general distribution, you’ll need to use ‘if’. You might also like to consider what to do if ‘p’ is a disk.

Hi there,

Can someone help me with this query.
Working with one of the previous scripts, if I run the script below I get the error:- Can’t make alias “folder1:folder2:folder3:file” into type constant.

on open (someitems)
	
	repeat with theitem in someitems
		
		set fullPath to (path to theitem) as string
		set AppleScript's text item delimiters to {":"}
		set containerPath to ((text items 1 through -3 of fullPath) as string) & ":"
		set AppleScript's text item delimiters to {""}
		
		display dialog containerPath
		
	end repeat
	
end open

Please can someone tell me why this is and how I solve it?

Thanks in advance,

Nick

Make this change:

set fullPath to theItem as string

theItem in someItems is already a path (as alias).

And by the way, in terms of elapsed time, this is about as fast and much simpler:

on open (someitems)
	repeat with theitem in someitems
		tell application "Finder" to set containerPath to (container of theitem) as string
		display dialog containerPath
	end repeat
end open

The Finder has improved a lot in Tiger and the original thread was for Panther.

Hi Adam,

Thanks for the prompt reply and for your help. :slight_smile:

I was actually using the latter in the script I’m on with at the mo however out of curiosity I started playing with the other.
Thanks for the second listing, it was useful - I’d been using the same but with an extra pair of brackets, so I’ve now been able to tidy that up.

Cheers!

Nick

Most welcome.

If you like “squishing them up”:

on open (someitems)
	repeat with theitem in someitems
		tell application "Finder" to display dialog container of theitem as string
	end repeat
end open