Searching for subfolders and setting path to folder

Hi All,

I am working on a script to allow the user to pick a folder. Once picked, I wanted a search of the subfolders to see if a folder exists. In my case it will be named “Proxy”.

Once I have found this folder, I would like to basically "Set Proxyfolder to path to “Proxy” folder. This way, I can perform an action to this particular folder.

Also, in many cases there may be more than one subfolder by the same name of “Proxy.” How would I know which folder I would be dealing with? So far, my code is like this:


property new_foldername : "PROXY"
set this_folder to (choose folder with prompt "Select the main shoot folder")

tell application "Finder"
	
	if (exists folder new_foldername of entire contents of this_folder) then
		set Proxypath to (POSIX path of new_foldername)
		-- here I would then perform an action to the folder
	else
		display dialog "Proxy folder is not found"
	end if
end tell

Thank you so much for your help.

Model: Mac Pro 10.5.7
AppleScript: 2.0.1
Browser: Safari 525.28.3
Operating System: Mac OS X (10.5)

Hi,

try this, it uses a shell command which is much faster than the Finder.
If there is more then one folder with the same name, you can choose from a list


property new_foldername : "PROXY"
set this_folder to POSIX path of (choose folder with prompt "Select the main shoot folder")

set found to paragraphs of (do shell script "/usr/bin/find " & quoted form of this_folder & " -type d -name " & quoted form of new_foldername)
set foundCount to count found
if foundCount is 0 then
	display dialog new_foldername & " folder is not found"
else if foundCount is 1 then
	set Proxypath to item 1 of found
else
	set newList to {}
	set {TID, text item delimiters} to {text item delimiters, "//"}
	repeat with i in found
		set end of newList to text item 2 of i
	end repeat
	set text item delimiters to TID
	
	set chosen to choose from list newList
	if chosen is false then return
	set Proxypath to this_folder & item 1 of chosen
end if

Hi Stefan,

Thank you for this great response. I had one more question regarding this. The second line says: Set this_folder to POSIX path of (choose folder with prompt “Select the main shoot folder”)

Is there a way that I could also use this as the text and later on name a file? Setting the folder name to text to be used as a filename? Thanks again!



property new_foldername : "PROXY"
set this_folder to POSIX path of (choose folder with prompt "Select the main shoot folder")

set found to paragraphs of (do shell script "/usr/bin/find " & quoted form of this_folder & " -type d -name " & quoted form of new_foldername)
set foundCount to count found
if foundCount is 0 then
   display dialog new_foldername & " folder is not found"
else if foundCount is 1 then
   set Proxypath to item 1 of found
else
   set newList to {}
   set {TID, text item delimiters} to {text item delimiters, "//"}
   repeat with i in found
       set end of newList to text item 2 of i
   end repeat
   set text item delimiters to TID
   
   set chosen to choose from list newList
   if chosen is false then return
   set Proxypath to this_folder & item 1 of chosen
end if

the POSIX path of an alias contains the full path (slash separated) to the folder.
You can retrieve the name of the folder with


set this_folder to (choose folder with prompt "Select the main shoot folder")
set folderName to name of (info for this_folder)
set folderPOSIXpath to POSIX path of this_folder

Hi Stefan,
I am trying to pull out all the mp4 files in a folder named “Proxy” the issue is, I keep getting an error: Can’t get every file of “/Users/boyd/Desktop/Test//Test-2/PROXY”. Here is the code I have so far…I have shown what line the issue occurs.


property new_foldername : "PROXY"
set this_folder to (choose folder with prompt "Select the main shoot folder")
set foldername to name of (info for this_folder)
set folderPOSIXpath to POSIX path of this_folder

set found to paragraphs of (do shell script "/usr/bin/find " & quoted form of folderPOSIXpath & " -type d -name " & quoted form of new_foldername)
set foundCount to count found
if foundCount is 0 then
	display dialog new_foldername & " folder is not found"
else if foundCount is 1 then
	set Proxypath to item 1 of found
	tell application "Finder"
		if (exists (files of Proxypath whose name ends with ".mp4")) then --[b]THIS IS THE LINE WITH THE ERROR[/b]
			set validextension to true
		else
			display dialog "There are no mp4 files in this folder." with icon stop
			set tries to tries + 1
		end if
	end tell
	if not validextension then
		display dialog "This program will now close." with icon note buttons {"OK"} default button 1
	else
		tell application "Finder" to set sorted_items ¬
			to files of Proxypath whose name ends with ".mp4"
		repeat with i from 1 to the count of sorted_items
			set this_item to (item i of sorted_items)
			tell application "QuickTime Player"
				if i is equal to 1 then
					make new document
				end if

AppleScript works with HFS paths (colon separated), so you have first to coerce the path


property new_foldername : "PROXY"
set this_folder to (choose folder with prompt "Select the main shoot folder")
set foldername to name of (info for this_folder)
set folderPOSIXpath to POSIX path of this_folder

set found to paragraphs of (do shell script "/usr/bin/find " & quoted form of folderPOSIXpath & " -type d -name " & quoted form of new_foldername)
set foundCount to count found
if foundCount is 0 then
	display dialog new_foldername & " folder is not found"
else if foundCount is 1 then
	set Proxypath to item 1 of found
	set HFSpath to POSIX file Proxypath
	tell application "Finder"
		if (exists (files of folder HFSpath whose name ends with ".mp4")) then --[b]THIS IS THE LINE WITH THE ERROR[/b]
			set validextension to true
		else
			display dialog "There are no mp4 files in this folder." with icon stop
			set tries to tries + 1
		end if
	end tell
	if not validextension then
		display dialog "This program will now close." with icon note buttons {"OK"} default button 1
	else
		tell application "Finder" to set sorted_items ¬
			to files of folder HFSpath whose name ends with ".mp4"
		repeat with i from 1 to the count of sorted_items
			set this_item to (item i of sorted_items)
			tell application "QuickTime Player"
				if i is equal to 1 then
					make new document
				end if
			end tell
		end repeat
	end if
end if

Hi Stefan,

That works! Thank you so much for your help. You guys have the most active Forum around. It is good to see.

Is there a terminal command that would make Quicktime run in the background while it is performing applescript tasks? Or an Applescript command for that matter? My problem is that when Applescript calls Quicktime, it continues to move to the front of the screen as it continues to receive new commands.

Thanks again,

activate another application or use this code, which does the same as the menu item “Hide” in any program menu


tell application "System Events" to set visible of process "QuickTime Player" to false

Hi Stefan,

When I have more than one proxy folder, I get an error that says, “Finder got an error: AppleEvent handler failed.”
This has to do with the line of code I have marked. I you can give me some advice, I would greatly appreciate it.



property new_foldername : "PROXY"
set this_folder to (choose folder with prompt "Select the main shoot folder")
set foldername to name of (info for this_folder)
set folderPOSIXpath to POSIX path of this_folder

set found to paragraphs of (do shell script "/usr/bin/find " & quoted form of folderPOSIXpath & " -type d -name " & quoted form of new_foldername)
set foundCount to count found
if foundCount is 0 then
	display dialog new_foldername & " folder is not found"
else if foundCount is 1 then
	set Proxypath to item 1 of found
	set HFSpath to POSIX file Proxypath
	
else
	set newList to {}
	set {TID, text item delimiters} to {text item delimiters, "//"}
	repeat with i in found
		set end of newList to text item 2 of i
	end repeat
	set text item delimiters to TID
	
	set chosen to choose from list newList
	if chosen is false then return
	set Proxypath to this_folder & item 1 of chosen
end if
tell application "Finder"
	[b]if (exists (files of folder HFSpath whose name ends with ".mp4")) then[/b] --This line of code gets the error
		set validextension to true
	else
		display dialog "There are no mp4 files in this folder." with icon stop
		set tries to tries + 1
	end if
end tell
if not validextension then
	display dialog "This program will now close." with icon note buttons {"OK"} default button 1
else


.
if foundCount is 0 then
	display dialog new_foldername & " folder is not found"
else if foundCount is 1 then
	set Proxypath to item 1 of found
else
	set newList to {}
	set {TID, text item delimiters} to {text item delimiters, "//"}
	repeat with i in found
		set end of newList to text item 2 of i
	end repeat
	set text item delimiters to TID
	
	set chosen to choose from list newList
	if chosen is false then return
	set Proxypath to this_folder & item 1 of chosen
end if
set HFSpath to POSIX file Proxypath
tell application "Finder"
	if (exists (files of folder HFSpath whose name ends with ".mp4")) then --This line of code gets the error
.

Hi Stefan,
The whole script almost works, but I am running into an error on the following code:

“Can’t get POSIX file {alias “HEC-TV 010:9.16/17.09 HEC Christin, Wally Smith Doc. :”, “9.16:17.09 HEC Christin, Wally Smith Doc. card 2/CONTENTS/PROXY”}.”


else
	set newList to {}
	set {TID, text item delimiters} to {text item delimiters, "//"}
	repeat with i in found
		set end of newList to text item 2 of i
	end repeat
	set text item delimiters to TID
	set chosen to choose from list newList
	if chosen is false then return
	
	set Proxypath to this_folder & item 1 of chosen
	
	set HFSpath to POSIX file Proxypath --THIS IS THE LINE WITH THE ERROR
	tell application "Finder"
		if (exists (files of folder HFSpath whose name ends with ".mp4")) then
			set validextension to true
		else
			display dialog "There are no mp4 files in this folder." with icon stop
			set tries to tries + 1
		end if
	end tell


as the beginning of the script has been changed replace the error line with


set Proxypath to folderPOSIXpath & item 1 of chosen