how do I find a parent folder's path?

First, I am a serious NOOB. So please forgive me.

Second, I am trying to make my scripts as portable as possible so that it can be distributed on flash card, cd etc. One thing I can’t figure out is how to do relative paths instead of absolute paths. If I insert a cd or flash card, I want the script to be able to draw a path to the parent folder, or parent of the parent etc. For example, if the directory structure is as follows:

folder1
scriptA.scpt
scriptB.scpt
scriptC.scpt
folder2
image1.jpg
image2.jpg
image3.jpg

I want scriptA to be able to point to image3.jpg without reference to the root level. So far, when I use “path to me” all I get is a path to the applescipt application, which is clearly not want I want.

if I can figure this out then I can do things like:

set path1 to (path to parent folder as text) & "folder2:image3.jpg"
  1. any bright ideas on how I can resolve this?

  2. Aside from using the “Dictionary” which I have not found very useful, any ideas where I can find better applescript reference material so I don’t bog down these forums with such basic questions? Something LIKE the dictionary but wih a quick example would be perfect. Does such a thing exist?

-i

The “Finder” knows…

set f to choose folder
tell application "Finder"
	set c to container of f as alias
	set cc to container of c as alias
	-- set cc to container of container of f as alias also works
end tell

Look here for references: http://applescriptcentral.com/

Here’s something reusable:

choose file without invisibles
getParentPath from (result) for -2

to getParentPath from _somePath for _depth
	-- _depth: -2, parent; -3, parent's parent; etc.
	if _somePath's class is alias then set _somePath to _somePath as Unicode text
	if _somePath's last character is ":" then set _depth to _depth - 1 -- handle folders too
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	set _somePath to (text 1 thru text item _depth of _somePath) & ":"
	set AppleScript's text item delimiters to ASTID
	return _somePath
end getParentPath

See also:
Moving to parent directory
Getting the parent of a file
Add parent folder of selected file to sidebar

Guys,

thanks for the reply.

both of these seem to rely on “choose.” While choose is very valuable is many cases, it isn’t something that I want to use in this case. I was hoping tha this script woudl run without the user knowing, meaning, I just want the script to be able to detect what its (being, the script itself) path is and then find its parent’s path. Bruce, your script did return the perfect path, but the “choose” functoin makes it impossible to use.

Am I making sense at all?

-i

iPS;

We both used “choose” because we didn’t have a better way to illustrate. The problem you have is that you’ve used Folder1 and Folder2, but don’t know where the “customer” might put Folder2 relative to Folder1. You haven’t told us much about the problem - things like who “owns” Folder2 - you or the “customer”. We don’t know what you’re doing, hence generic answers. Your directory structure shows two folders. Are they in the same container?

Thanks Adam, I understand your point. With my limited knowledge of Applescript I am hoping that it doesn’t matter where the folders are relative to the root.

The user will not be able to change the folder structure. So, in theory, I could hardcode each of the files from the final projects VOLUME. CD-ROM:file1:

But, since I am testing it in a totally different folder G5HD:Users:me:testFolder:folder1 I want to be able to build a script that is independent of absolute directory structure. I just want a relative path starting FROM the script itself.

something like this from Bruce’s script above?

getParentPath from (thisScript'sPath) for -2

My solution might not be as useful, but I was simply trying to rewrite

set path1 to (path to parent folder as text) & "folder2:image3.jpg"

I came up with this, assuming that an image is selected

tell application "Finder"
	set theImage to "folder2:image3.jpg"
	set theContainer to (container of target of front window as text)
	set path1 to theContainer & theImage
	display dialog path1--this just shows the result
end tell

It doesn’t seem to work on files selected on the desktop, only in finder windows.

If your script is saved as an application, then the line:

path to me

will give the path to your scipt,

container of (path to me)

should give the enclosing folder.

If your script is not saved as an application, the above will always give the path to the Scipt Editor application

Andy

Browser: Safari 412
Operating System: Mac OS X (10.4)

Like Adam said, that was just an example.

Here’s another example that uses a repeat loop:

set fileList to {"Whatever:folder1:scriptA.scpt", "Whatever:folder1:scriptB.scpt", "Whatever:folder1:scriptC.scpt", "Whatever:folder2:image1.jpg", "Whatever:folder2:image2.jpg", "Whatever:folder2:image3.jpg"}

repeat with thisItem in fileList
	set thisParent to getParentPath from thisItem for -2
	-- do whatever you're going to do with the file and it's parent
	
	-- this is just so you can see that the value from the getParentPath handler 
	display dialog thisParent
end repeat

to getParentPath from _somePath for _depth
	-- _depth: -2, parent; -3, parent's parent; etc.
	if _somePath's class is alias then set _somePath to _somePath as Unicode text
	if _somePath's last character is ":" then set _depth to _depth - 1 -- handle folders too
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	set _somePath to (text 1 thru text item _depth of _somePath) & ":"
	set AppleScript's text item delimiters to ASTID
	return _somePath
end getParentPath

We might be able to give you more information if we knew exactly what you’re doing with scriptA and image3.

Okay,

Thanks all.

Basically, all I am doing is running a Flash app from inside a CD - flash card etc… The Flash app calls our Applescript which simply open a JPG which is, now, in a sub folder of where the script resides.

okay, that made all the difference. So I can’t run this script from within script editor, I always have to savev it and double click on the app from now on. Bother.

Here is the script that I have that works now:

set myFile to ("jpgs:eat.jpg")
set path1 to getParentPath from (path to me) for -2

tell application "Finder"
	activate
	display dialog path1 & myFile
	open document file (path1 & myFile)
end tell

to getParentPath from _somePath for _depth
	-- _depth: -2, parent; -3, parent's parent; etc.
	if _somePath's class is alias then set _somePath to _somePath as Unicode text
	if _somePath's last character is ":" then set _depth to _depth - 1 -- handle folders too
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	set _somePath to (text 1 thru text item _depth of _somePath) & ":"
	set AppleScript's text item delimiters to ASTID
	return _somePath
end getParentPath

Frankly, I am just amazed that there has to be so many lines of code to open an image on a file that is just one directory down. I admitedly don’t really understand anything beneath Bruce’s “to getParentPath from _somePath for _depth”

I get the subroutine idea, but all of the checks, “ifs” and “sets” seem like a lot of work for finding the directory. Looks like I will need to poke around a bit to understand that code.

Thanks tons!

-i

Here’s something shorter:

set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {":"}}
set path1 to text 1 thru text item -3 of (path to me as Unicode text) & ":"
set AppleScript's text item delimiters to ASTID

tell application "Finder" to open alias (path1 & "jpgs:eat.jpg")

An alternative:

tell application "Finder"
	get container of (path to me)
	open file "eat.jpg" of folder "jpgs" of result
end tell

Here’s an article on AppleScript’s text item delimiters: Using AppleScript’s Text Item Delimiters for Text Manipulations

No bother, Alastair. If you want Script Editor to run the script (once it has been saved at least once - to give it a location), then something like this should do the trick:

set myFile to "jpgs:eat.jpg"
tell application "Script Editor" to set script_file to my POSIX file (front document's path)
tell application "Finder" to open file ((script_file's container as Unicode text) & myFile)

(Incidentally, I enjoyed browsing your site the other day. You have some great shots, there - and the web design is really neat, too. :))

Let me second that, bigtime. Lovely site, lovely photos.

Ahhhhhhhhhhhhhhhhh. Like a breath of fresh air! Thanks folks. That is perfect.

Kai, Adam, thanks for the complement. I must admit I haven’t touched it in about three years. It’s time for a redesign!!! I’ve become a bit more functional in my design style, and some of those photos need to be pulled and replaced with others for sure!!

-i

Just for the record. my handler (and text item delimiters) doesn’t necessarily get the parent object of an alias (like the Finder’s container property does); An alias is coerced to (some sort of) text, said text is manipulated, and then the manipluated text is coerced back into an alias.