Get folder path somewhere other than finder window

This script contains code to create two folders, “High Res” and “Low Res”

The script only creates the two folders properly if the finder window is open to the right directory because it uses “target of front finder window”

Can someone help me make it so that it’s not using the finder window to determine the folder path? So that it creates the two directories, even if I don’t have any finder windows open?

Here’s the script:

on open of myFiles

tell application "Finder"
	set p to (target of front Finder window) as text
	make new folder at p with properties {name:"Low Res"}
	make new folder at p with properties {name:"High Res"}
end tell

set newSizes to {2048}

repeat with aFile in myFiles
	set filePath to aFile's POSIX path
	set bPath to (do shell script "dirname " & quoted form of filePath)
	tell application "System Events" to set fileName to aFile's name
	repeat with newSize in newSizes
		do shell script "sips " & quoted form of aFile's POSIX path & " -Z " & newSize & " --out " & quoted form of (bPath & "/Low Res/" & rename(fileName, "low-res") as text)
	end repeat
end repeat

set newSizes to {5000}

repeat with aFile in myFiles
	set filePath to aFile's POSIX path
	set bPath to (do shell script "dirname " & quoted form of filePath)
	tell application "System Events" to set fileName to aFile's name
	repeat with newSize in newSizes
		do shell script "sips " & quoted form of aFile's POSIX path & " -Z " & newSize & " --out " & quoted form of (bPath & "/High Res/" & rename(fileName, "high-res") as text)
	end repeat
end repeat

set bPath2 to POSIX file bPath as text
tell application "Finder"
	delete (every document file of folder bPath2 whose name ends with ".jpg")
end tell

end open

on rename(fName, fSize)
do shell script “sed 's/high-res/” & fSize & "/’ <<< " & quoted form of fName
end rename

Hi,

replace


	tell application "Finder"
		set p to (target of front Finder window) as text
		make new folder at p with properties {name:"Low Res"}
		make new folder at p with properties {name:"High Res"}
	end tell

with

set theFolder to choose folder
checkFolder(theFolder, "Low Res")
checkFolder(theFolder, "High Res")

and put this handler at the end of the script after the end rename line

on checkFolder(parentFolder, folderName)
	tell application "Finder"
		if not (exists folder folderName of parentFolder) then
			make folder at parentFolder with properties {name:folderName}
		end if
	end tell
end checkFolder

Stefan,

Thanks for the quick reply. I am seeing a bug though that I can’t figure out - When I run the script it does not create the folders as desired and throws an error dialog - “Can’t continue checkFolder”

Here is my updated code from your suggestion:

on open of myFiles

tell application "Finder"
	set theFolder to choose folder
	checkFolder(theFolder, "Low Res")
	checkFolder(theFolder, "High Res")
end tell

set newSizes to {2048}

repeat with aFile in myFiles
	set filePath to aFile's POSIX path
	set bPath to (do shell script "dirname " & quoted form of filePath)
	tell application "System Events" to set fileName to aFile's name
	repeat with newSize in newSizes
		do shell script "sips " & quoted form of aFile's POSIX path & " -Z " & newSize & " --out " & quoted form of (bPath & "/Low Res/" & rename(fileName, "low-res") as text)
	end repeat
end repeat

set newSizes to {5000}

repeat with aFile in myFiles
	set filePath to aFile's POSIX path
	set bPath to (do shell script "dirname " & quoted form of filePath)
	tell application "System Events" to set fileName to aFile's name
	repeat with newSize in newSizes
		do shell script "sips " & quoted form of aFile's POSIX path & " -Z " & newSize & " --out " & quoted form of (bPath & "/High Res/" & rename(fileName, "high-res") as text)
	end repeat
end repeat

set bPath2 to POSIX file bPath as text
tell application "Finder"
	delete (every document file of folder bPath2 whose name ends with ".jpg")
end tell

end open

on rename(fName, fSize)
do shell script “sed 's/high-res/” & fSize & "/’ <<< " & quoted form of fName
end rename

on checkFolder(parentFolder, folderName)
tell application “Finder”
if not (exists folder folderName of parentFolder) then
make folder at parentFolder with properties {name:folderName}
end if
end tell
end checkFolder

the replacement part does not contain the Finder tell block


on open of myFiles
    
        set theFolder to choose folder
        checkFolder(theFolder, "Low Res")
        checkFolder(theFolder, "High Res")

        set newSizes to {2048}
   -- .

Awesome thanks!

Now is there anyway to avoid having to choose the folder? Can the script determine the folder path it’s already in and not have to prompt me to choose it?

as you don’t need the run handler use this, doubleclick the applet to change the folder


property theFolder : missing value

on open myFiles
	if theFolder is missing value then set theFolder to choose folder
	
	checkFolder(theFolder, "Low Res")
	checkFolder(theFolder, "High Res")
	
	set newSizes to {2048}
	-- .
	
end open

on run
	set theFolder to choose folder
end run

This solution still has the same prompt to choose a folder. When I’m in Lightroom I choose the folder and it goes and exports these files there. I’d rather not have to choose the folder a 2nd time. Since the script is already writing the files without needing a ‘choose folder prompt’, can’t it use that same knowledge to create the folders without a ‘choose folder prompt’?

Thanks so much for your help on this!!

if you save the script as application to be able to drag files onto it, the solution is supposed to work

So in Adobe Lightroom, in the export settings, you can choose a program to run when the export is finished. So the way that the script is currently being opened is by Lightroom. Am trying to make it so I don’t have to open up Finder at all for it to work.

Sorry, I don’t have Lightroom, I have no idea what kind of event is sent to launch the target application

If it’s any help, before I started creating new folders, the image resizing and saving worked fine. So the following code created new jpg files without me having to choose folders. (other than the folder choice I make within Lightroom)

set newSizes to {2048}

repeat with aFile in myFiles
	set filePath to aFile's POSIX path
	set bPath to (do shell script "dirname " & quoted form of filePath)
	tell application "System Events" to set fileName to aFile's name
	repeat with newSize in newSizes
		do shell script "sips " & quoted form of aFile's POSIX path & " -Z " & newSize & " --out " & quoted form of (bPath & "/Low Res/" & rename(fileName, "low-res") as text)
	end repeat
end repeat

So is there any way to use this same technique to grab the path for the folder creation?

is your script a standard script file (.scpt) or an application (.app) ?

.app

then the solution with the starting property line must work

It actually does work 100% if the folders are already there. But if it’s my first time exporting photos to this folder and the Low Res and High Res folders are not yet created, then it prompts me to choose a folder.

This is not the end of the world because it still does most of the thinking for me, but would be nice if in both cases, whether it’s a brand new export, or a re-export, it could not prompt for that choose folder, and could work from the folder I choose within Lightroom.

After a closer look at the script I noticed that the created folders are not used at all, because the sips lines take the parent folder of each dropped file as “theFolder”, which is the normal behavior using “target of front finder window”.

Well I can get the path, but it doesn’t seem to be in the right format. In theory the following code should do what I’m wanting, but am getting the error - Can’t make “Low Res” into type integer.

Can you help me maybe just format the “bPath” variable so that it works in the checkFolder function?

Here is my code:

on open of myFiles

set newSizes to {2048}

repeat with aFile in myFiles
	set filePath to aFile's POSIX path
	set bPath to (do shell script "dirname " & quoted form of filePath)
	checkFolder(bPath, "Low Res")
	tell application "System Events" to set fileName to aFile's name
	repeat with newSize in newSizes
		do shell script "sips " & quoted form of aFile's POSIX path & " -Z " & newSize & " --out " & quoted form of (bPath & "/Low Res/" & rename(fileName, "low-res") as text)
	end repeat
end repeat

set newSizes to {5000}

repeat with aFile in myFiles
	set filePath to aFile's POSIX path
	set bPath to (do shell script "dirname " & quoted form of filePath)
	checkFolder(bPath, "High Res")
	tell application "System Events" to set fileName to aFile's name
	repeat with newSize in newSizes
		do shell script "sips " & quoted form of aFile's POSIX path & " -Z " & newSize & " --out " & quoted form of (bPath & "/High Res/" & rename(fileName, "high-res") as text)
	end repeat
end repeat

set bPath2 to POSIX file bPath as text
tell application "Finder"
	delete (every document file of folder bPath2 whose name ends with ".jpg")
end tell

end open

on rename(fName, fSize)
do shell script “sed 's/high-res/” & fSize & "/’ <<< " & quoted form of fName
end rename

on checkFolder(parentFolder, folderName)
tell application “Finder”
if not (exists folder folderName of parentFolder) then
make folder at parentFolder with properties {name:folderName}
end if
end tell
end checkFolder

try this optimized version, the mkdir shell command creates the folders if needed, the check is for free


on open myFiles
	set lowSizes to {2048}
	set highSizes to {5000}
	
	repeat with aFile in myFiles
		tell application "System Events"
			set {name:fileName, POSIX path:POSIXpath, container:parentFolder} to aFile
			set POSIXparentFolder to POSIX path of parentFolder
			set HFSparentFolder to path of parentFolder
		end tell
		do shell script "/bin/mkdir -p " & quoted form of POSIXparentFolder & "/{'Low Res','High Res'}"
		repeat with newSize in lowSizes
			do shell script "sips " & quoted form of POSIXpath & " -Z " & newSize & " --out " & quoted form of (POSIXparentFolder & "/Low Res/" & rename(fileName, "low-res"))
		end repeat
		repeat with newSize in highSizes
			do shell script "sips " & quoted form of POSIXpath & " -Z " & newSize & " --out " & quoted form of (POSIXparentFolder & "/High Res/" & rename(fileName, "high-res"))
		end repeat
	end repeat
	
	tell application "Finder"
		delete (every document file of folder HFSparentFolder whose name ends with ".jpg")
	end tell
	
end open

on rename(fName, fSize)
	do shell script "sed 's/high-res/" & fSize & "/' <<< " & quoted form of fName
end rename

Really nice code. It has a couple issues, although I’m concerned with how much of your time I’ve already taken!

The issues are:

  1. the delete command is not working. gets an error "Can’t get <> “path to jpg” of application “Finder”.
  2. With the code we had before, I was able to re-export photos I had previously exported, and they would simply overwrite the old file. Now it doesn’t overwrite the older versions. It seems to see there is a file there so it doesn’t save a new one.

In the last code I had, is there anyway to just reformat that path issue so that it works in the checkFolder function? I realize that code is not completely optimized but it seems like that one little thing is the only thing holding it up.

Actually I was wrong about the issue #2 so please disregard.

So yeah, the only issue, is the delete doesn’t work. It has that error code…