Hi All,
I have a list of file paths that I acquired from a plist file for an application I use. My goal is to test every item in the list and keep the ones that are valid. Here is what I have so far
-- SessionRefArray is the list of files that need to be tested. This is the form they are read from the plist file
-- For testing change the first one of these to a file that exists on your desktop
set SessionRefArray to {"file://localhost/Users/mtorrance/Desktop/some_file.JPG", "file://localhost/Volumes/Central%20Storage/Work%20Storage/Potential%20Stock/Potential%20Stock.session", "file://localhost/Volumes/Central%20Storage/Work%20Storage/Torrance%20X-Mas%20Card%202007/Torrance%20X-Mas%20Card%202007.session", "file://localhost/Users/marktorrance/Pictures/Capture%20One%20Default%20Session/Some.session", "file://localhost/Volumes/Central%20Storage/Work%20Storage/0702-11%20Mazzios/0702-11%20Mazzios.session", "file://localhost/Volumes/Central%20Storage/Work%20Storage/0712-02%20BOK/0712-02%20BOK.session"} as list
-- Get the name of the startup disk
tell application "Finder"
get name of startup disk of application "Finder"
end tell
set startupDisk to result
set filteredSessionRefArray to {}
--Keep only file paths that still exist
repeat with i in SessionRefArray
-- Make the path HFS
set testPath to i
set testPath to my replace_chars(i, "%20", " ")
set testPath to my replace_chars(testPath, "file://localhost", "")
set testPath to my replace_chars(testPath, "/", ":")
set testPath to startupDisk & testPath as string
--return testPath -- returns "Marks G4 HD:Users:marktorrance:Pictures:Capture One Default Session:Default.session" from "file://localhost/Users/marktorrance/Pictures/Capture%20One%20Default%20Session/Default.session" in my case
-- Run handler to test for files existence
my fileExists(testPath)
set myResult to result
if myResult is true then -- if the file exists
display dialog "Yes"
-- Add the corrisponding "file://" path to the list filteredSessionRefArray
else
-- Don't add item to list and go to next item in SessionRefArray
display dialog "No"
end if
end repeat
-- For me it tested every item properly. The first existed but not the others
-- So how do I save each one that tests true in the list filteredSessionRefArray?
return filteredSessionRefArray -- At this point I should have a list of only the file paths that tested true
-- Handler to replace characters
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
-- Handler to test if file exists
on fileExists(testPath)
try
testPath as string as alias
return true
on error
return false
end try
end fileExists
It tests each path correctly but how do I retain the valid paths in a variable named filteredSessionRefArray. In this test the variable filteredSessionRefArray should be a list with the one item that existed but should contain any items that tested true.
{“file://localhost/Users/mtorrance/Desktop/some_file.JPG”}
Many Thanks,
MT