How to reach grandparent folder of parent folder of current folder

Hi,

Is there any way we can define parent of Parent and child of Child folders through applescript:

Suppose, my folder structure is:

and my current folder is “Folder4”

If I have a file in “Folder4”, I can get to “Folder3” with:

set sourcePath to the file path of myDocument as string

Similarly what will be the codes to reach to “Folder2” and “Folder1”
and if I do not have a file in “Folder4” than what will be the code to get to its parent “Folder3”

Secondly, If I want to save a file from Folder4 to Folder5 then I can write as:


        set HighPDF to (Folder5 as string) & sourceName & ".pdf" as string
       export format PDF type to HighPDF using "CSP_EXPORT_3.20.14" without showing options

but what will be the code to save the file in Folder6 or Folder7

Any ideas?

Sure.

Where is file path coming from? Is that a legitimate property of your app - and what app might that be? It is not a normal AppleScript property.

In any case getting the file-path will not get you to container of container of the-file.

set _file to alias "Ryoko:Users:chris:Downloads:Guido.txt"

tell application "Finder"
	if (class of (get properties of _file)) = document file then
		set _fldr to _file's container as alias
	end if
end tell

After getting the file’s container you can go to town.

set _fldr to alias "Ryoko:Users:chris:Downloads:"

tell application "Finder"
	set newLocation to (_fldr's parent's parent) as alias
end tell

OR (using Text Item Delimiters instead of the Finder):

set _fldr to alias "Ryoko:Users:chris:Downloads:"

set newLocation to prevFldrIndex(_fldr, -2)

on prevFldrIndex(_alias, _index)
	try
		set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
		set _path to _alias as text
		if _path ends with ":" then
			set _path to text 1 thru -2 of _path
		else
			error "Object is not a folder!"
		end if
		set prevFldr to ((text items 1 thru _index of _path) as text) & ":"
		set AppleScript's text item delimiters to oldTIDS
		return prevFldr
	on error e
		set AppleScript's text item delimiters to oldTIDS
		error e
	end try
end prevFldrIndex

set _fldr to alias "Ryoko:Users:chris:Downloads:"

tell application "Finder"
	parent of _fldr as alias
end tell

You’ve already figured out that you can use the path here.

Basically do you the same thing: folder_path/file_name.

What’s your hang up? Do you not know the names of folder6 & 7?

Once you have a file-path as a string be that an HFS-Path or a Posix-Path you can go backwards all you want, but if you want to go forwards you have to either know the name of the next node, or detect it, or create it.

Hi Chris.

That’s interesting. ‘parent’ does work, but it’s not in the Finder’s dictionary. The usual term is ‘container’, as in your first script. On the other hand, ‘parent’ ” which is a vanilla term signifying an inheritance relationship between script objects ” doesn’t work as a ‘container’ substitute outside the Finder context, so it must mean actually something to the Finder. Intriguing . :confused:

Hey Nigel,

I’ve been using parent for many years in the Finder context.

Hey Nigel,

Well I’ll be. This works too:

set _file to alias "Ryoko:Users:chris:Downloads:Guido.txt"

tell application "Finder"
	set _parent to _file's parent as alias
end tell

I’m not certain I’ve ever tried this before.

Hi Chris.

Yeah. Looking back through some of the old posts here and on AppleScript-Users, I see quite a few people ” many of them quite respectable! :wink: ” have used it at various times. I’ve just never clocked before. It’s definitely never been in any Finder dictionary I’ve ever seen. Maybe it’s a hangover from something in the distant past (ie. pre 1998).

Thanks everyone. I’'ll do some RnD and get back to you with results.

Hi, I have a script to create JPEGs from a PDF that is in the current finder window:

tell application "Finder"
	set theLocation to (target of front window) as string
	set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
	set pageWidth to pageWidth as real
	
	try
		set afile to (1st file of folder theLocation whose name extension is "pdf")
		set thefile to afile as string
		
	end try
	
	tell application "Adobe Photoshop CS5"
		set display dialogs to never
		set myOptions to {class:PDF open options, mode:RGB, resolution:300, use antialias:true, page:1, constrain proportions:true, crop page:bounding box}
		open file thefile as PDF with options myOptions
		set mystate to current history state of current document
		
		set docRef to current document
		set docName to name of docRef
		set BackC to (theLocation as string) & docName & "_Back Cover"
		set FrontC to (theLocation as string) & docName & "_Front Cover"
		
		resize canvas document 1 width pageWidth as inches anchor position middle left
		set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
		save docRef in file BackC as JPEG with options jpgOptions with copying
		set current history state of current document to mystate
		
		resize canvas document 1 width pageWidth as inches anchor position middle right
		set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
		save docRef in file FrontC as JPEG with options jpgOptions with copying
		close docRef without saving
		
	end tell
end tell

The script is working fine, however, if create a sub-folder in the current folder and tried to save the new jpegs in that subfolder, then I’m failing, here is the script:

tell application "Finder"
	set theLocation to (target of front window) as string
	set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
	set pageWidth to pageWidth as real
	
	try
		set afile to (1st file of folder theLocation whose name extension is "pdf")
		set thefile to afile as string
		
	end try
	
	tell application "Adobe Photoshop CS5"
		set display dialogs to never
		set myOptions to {class:PDF open options, mode:RGB, resolution:300, use antialias:true, page:1, constrain proportions:true, crop page:bounding box}
		open file thefile as PDF with options myOptions
		set mystate to current history state of current document
		
		set docRef to current document
		set docName to name of docRef

		tell application "Finder"
			set JpgFolder to "Hi-Res JPEG_" & docName
			make new folder at theLocation with properties {name:JpgFolder}
			
			set BackC to (JpgFolder as string) & docName & "_Back Cover"
			set FrontC to (JpgFolder as string) & docName & "_Front Cover"
		end tell

		resize canvas document 1 width pageWidth as inches anchor position middle left
		set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
		save docRef in file BackC as JPEG with options jpgOptions with copying
		set current history state of current document to mystate
		
		resize canvas document 1 width pageWidth as inches anchor position middle right
		set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
		save docRef in file FrontC as JPEG with options jpgOptions with copying
		close docRef without saving
		
	end tell
end tell

Can you please allow my script to save the jpegs in the new created sub-folder

AppleScript expects HFS paths (always starting with a disk name and colon separaed),
for example the path to Disk Utility in the Utilities folder is

MacHD:Applications:Utilities:Disk Utility.app

MacHD represents the name of the startup disk

Okay, but what will be the solution in my case:

        tell application "Finder"
           set JpgFolder to "Hi-Res JPEG_" & docName
           make new folder at theLocation with properties {name:JpgFolder}
           
           set BackC to (JpgFolder as string) & docName & "_Back Cover"
           set FrontC to (JpgFolder as string) & docName & "_Front Cover"
       end tell
....
....
....
        save docRef in file BackC as JPEG with options jpgOptions with copying
....
....
....
        save docRef in file FrontC as JPEG with options jpgOptions with copying

*see complete script, this is an excerpt

In my AppleScript, I have created a sub-folder inside the current folder and I then want to save the newly created JPEGs in that sub-folder. Any clues?

make new folder returns the object reference to the created folder so you could write


tell application "Finder"
	set JpgFolderName to "Hi-Res JPEG_" & docName
	set JpgFolder to (make new folder at theLocation with properties {name:JpgFolderName})
	
	set BackC to (JpgFolder as string) & docName & "_Back Cover"
	set FrontC to (JpgFolder as string) & docName & "_Front Cover"
end tell

Million thanks for all your help. My script is now complete. This one solution has solved my queries in two posts.

The other post, related to this problem is:
http://macscripter.net/viewtopic.php?id=42646

Once again THANKS A LOT :slight_smile: