if and partial file name problem when searching network drives

I have a network server that has pictures on it and i am trying to improve on a automator workflow that i have but takes forever to locate the pictures. There are about 470Gb of pictures. I’m trying to speed up response times by having the script direct the search to the correct year folder. The pictures are in folders that follow the format (year-assigned number-id of person, i.e. 08-123456-1234). I’m trying to find and open (reveal) this folder. What i have works but i have two problems. 1. How do i open file 08-123456, without knowing the last four digits (it will always be 4 digits); 2. i can’t seem to set the variable searchFolder inside an if statement. I type it in, but unless i’m missing something obivious i can’t get the variable declared inside the if statement which makes no sense. Below is my mostly working code. The if statement is commented out and as i said, i have to put in the entire file name because i can’t figure out how to use “starts with” for the file alias.



-- gets case number and returns it as a record and then converts it to text

(*

display dialog "What is the Case number" default answer "" buttons {"F2-Go..."} default button 1
set caseId to (text returned of result) as text
log caseId

--determines what file to search based upon two digit year.

set caseYear to characters 1 thru 2 of caseId
log caseYear as string

choose from list {"PlayScene$:2007 play Photos:", "PlayScene$:2009 Play Photos:", "PlayScene$:2009 Play Photos:"} with prompt "Choose Year"
set searchFolder to result
*)


(* 
the if conditions don't  declare the searchFolder variable. i don't know why

if caseYear is equal to "07" then
	set searchFolder to alias of "PlayScene$:2007 play Photos:"as string
else if caseYear is equal to "08" then
	set searchFolder to alias of "PlayScene$:2009 Play Photos:" as string
else if caseYear is equal to "09" then
	set searchFolder to "PlayScene$:2009 Play Photos:" as string
else if caseYear is equal to "10" then
	set searchFolder to alias of "PlayScene$:2010 Play Photos:" as string
end if
*)
-- the next two lines are used so that i can get this two work.
set caseId to "09-001004-1234"
set searchFolder to "PlayScene$:2009 Play Photos:"

log searchFolder

-- searches for and displays the picture files
tell application "Finder"
	set pictureWindow to (searchFolder & caseId as string) -- incomplete file name. the complete file actually has a -1234 number after it that is the persons id who uploaded the pictures.
	log class of pictureWindow
	
	reveal folder (pictureWindow as alias) -- incomplete file name. the complete file actually has a -1234 number after it that is the persons id who uploaded the pictures.
	log class of pictureWindow
	activate
end tell

Hi,

your if - then part doesn’t work, because the line

set caseYear to characters 1 thru 2 of caseId

results for example {“0”, “8”}, which is a list of two characters for entering 08-xxxx. To get “08”, change the line to

set caseYear to text 1 thru 2 of caseId

Ironically your log line works because text 1 thru 2 of caseId is the same as characters 1 thru 2 of caseId as string.

To find a folder on a shared volume with wildcards it’s preferable to use the shell find command.

Your choose from list part will raise another problem, because the result is a list containing the chosen item(s)

Thanks for the help. The if part was really bothering me.

I forgot to comment out the “choose from list” part, but that was put in simply because i couldn’t get the if statement to work. So that is gone now that the if statement works.

Frankly the find shell script is what i was trying to stay away from. i don’t understand them. I’ll take a look later and see if i can figure it out by borrowing something from somewhere else.

Try something like this


set baseFolder to quoted form of POSIX path of (choose folder with prompt "Choose base folder")
set {text returned:caseID} to display dialog "What is the Case number" default answer "" buttons {"F2-Go..."} default button 1
set foundFolders to do shell script "/usr/bin/find " & baseFolder & " -type d -name " & caseID & "*"
repeat with oneFolder in (get paragraphs of foundFolders)
	tell application "Finder" to reveal folder (POSIX file (contents of oneFolder) as text)
end repeat

Thanks for the help! It works pretty good now. Is there a way to get an the system to display how long the script took to run, as opposed to using a stopwatch. The old automator script that this replaced took over 6 minutes to search before i quit, while this script took only 1 min.



-- gets case number and returns it as a record and then converts it to text

display dialog "What is the Case number" default answer "" buttons {"F2-Go..."} default button 1
set caseId to (text returned of result) as text
log caseId

--determines what file to search based upon two digit year.

set caseYear to text 1 thru 2 of caseId
log caseYear as string

if caseYear is equal to "07" then
	set searchFolder to alias of "PlayScene$:2007 play Photos:" as string
else if caseYear is equal to "08" then
	set searchFolder to alias of "PlayScene$:2009 Play Photos:" as string
else if caseYear is equal to "09" then
	set searchFolder to "PlayScene$:2009 Play Photos:" as string
else if caseYear is equal to "10" then
	set searchFolder to alias of "PlayScene$:2010 Play Photos:" as string
end if


log searchFolder

-- searches for and displays the picture files. 

set baseFolder to quoted form of POSIX path of (searchFolder)
log baseFolder
set foundFolders to do shell script "/usr/bin/find " & baseFolder & " -type d -name " & caseId & "*"
repeat with oneFolder in (get paragraphs of foundFolders)
	tell application "Finder" to open folder (POSIX file (contents of oneFolder) as text)
	
end repeat

--set activated window to coverflow and select the first JPEG image
tell application "Finder"
	get name of Finder window 1
	set winRef to Finder window 1
	select winRef
	set bounds of winRef to {1719, 42, 2559, 594}
	set sort direction of column id name column of list view options of winRef to normal
	set current view of winRef to flow view
	select (first item of winRef whose kind is "JPEG image")
end tell


set t1 to current date

-- your script goes here

set t2 to current date
display dialog "the script took " & (t2 - t1) & " seconds" buttons {"Done"} default button 1

Do this line really work without any error message?

set searchFolder to alias of "PlayScene$:2007 play Photos:" as string

Actually if you want the alias use this syntax

set searchFolder to alias "PlayScene$:2007 play Photos:"

or if you want the string path no further coercion is needed

set searchFolder to "PlayScene$:2007 play Photos:"

You’re right. After i uploaded, i saved it as an app and then ran a test with another year other than 09. i just got done running it and it didn’t work. I just tested it with 2007 and it doesn’t work until i deleted the “alias of” line. You’re fast. Here is the corrected code.



-- gets case number and returns it as a record and then converts it to text

display dialog "What is the Case number" default answer "" buttons {"F2-Go..."} default button 1
set caseId to (text returned of result) as text
log caseId

--determines what file to search based upon two digit year.

set caseYear to text 1 thru 2 of caseId
log caseYear as string

if caseYear is equal to "07" then
	set searchFolder to "PlayScene$:2007 play Photos:" as string
else if caseYear is equal to "08" then
	set searchFolder to "PlayScene$:2008 Play Photos:" as string
else if caseYear is equal to "09" then
	set searchFolder to "PlayScene$:2009 Play Photos:" as string
else if caseYear is equal to "10" then
	set searchFolder to "PlayScene$:2010 Play Photos:" as string
end if


log searchFolder

-- searches for and displays the picture files. 

set baseFolder to quoted form of POSIX path of (searchFolder)
log baseFolder
set foundFolders to do shell script "/usr/bin/find " & baseFolder & " -type d -name " & caseId & "*"
repeat with oneFolder in (get paragraphs of foundFolders)
	tell application "Finder" to open folder (POSIX file (contents of oneFolder) as text)
	
end repeat

--set activated window to coverflow and select the first JPEG image
tell application "Finder"
	get name of Finder window 1
	set winRef to Finder window 1
	select winRef
	set bounds of winRef to {1719, 42, 2559, 594}
	set sort direction of column id name column of list view options of winRef to normal
	set current view of winRef to flow view
	select (first item of winRef whose kind is "JPEG image")
end tell


set searchFolder to "PlayScene$:2007 play Photos:"

no as string coercion!!!
as the name implies a literal string is string

thanks for the time hint.

PS: you could replace


f caseYear is equal to "07" then
   set searchFolder to "PlayScene$:2007 play Photos:" as string
else if caseYear is equal to "08" then
   set searchFolder to "PlayScene$:2008 Play Photos:" as string
else if caseYear is equal to "09" then
   set searchFolder to "PlayScene$:2009 Play Photos:" as string
else if caseYear is equal to "10" then
   set searchFolder to "PlayScene$:2010 Play Photos:" as string
end if

with


set caseyearInt to caseyear as integer
if caseyearInt > 6 and caseyearInt < 11 then
	set searchFolder to "PlayScene$:20" & caseyear & " play Photos:"
end if

Interesting. I just took out the string coercion and ran it again. I’ll make the other change too. That should be faster? or just cleaner? How long have you been scripting?

resolving if - then constructions is always faster and useless coercions are just dispensable.

I learned AppleScript three years ago and learned means really like a foreign language

Well that’s amazing as you seem to be one of the major contributors here. I’ll admit scripting seems to be pretty fun and is making several things at work easier.

It is, you’re absolutely right :slight_smile: