I upgraded from 10.4.9 to 10.4.11 and now I have a line of code that times out the apple event.
I use this line in a number of my codes and they all worked in 10.4.9
First I set extention_list and targFolder as so.
property extension_list : {“mp4”}
set targFolder to “Macintosh HD:Users:username:Music:iTunes:iTunes Music:” as alias
I error out on
tell application “Finder” to set vidList to get (name of files of (entire contents of (targFolder))) whose name extension is in the extension_list
When I run the codes now I get the following error.
“Finder got an error: AppleEvent timed out.”
The Whole Code is, This is not working in 10.4.11
property extension_list : {"mp4"}
set targFolder to "Macintosh HD:Users:username:Music:iTunes:iTunes Music:" as alias
set Filename to "Video List2" --Name the file
set target_file to "Macintosh HD:Users:username:Desktop:" & Filename
set append_data to true --Set this to false if you want to overwrite, true if you want to add text to an existing file this has to be true for the repeat, otherwise you end up with just the last name.
tell application "Finder" to set vidList to get (name of files of (entire contents of (targFolder))) whose name extension is in the extension_list
try --This part writes the file
set the target_file to the target_file as text
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
repeat with i from 1 to number of items in vidList --new
set theItem to (item i of vidList & return) --new
--write this_data to the open_target_file starting at eof
write theItem to the open_target_file starting at eof --new
end repeat --new
close access the open_target_file
on error
try
close access file target_file
end try
end try
display dialog "I have finished making the video list"
searching in entire contents with the Finder can take a very long time.
AppleScript has a built-in timeout of about 2 minutes.
Either you wrap the crucial line with a timeout block to control the timeout like
.
with timeout of 10 * minutes seconds
tell application "Finder" to set vidList to get (name of files of (entire contents of (targFolder))) whose name extension is in the extension_list
end timeout
.
or you use Spotlight to find the files, which is incredibly faster
property extension_list : {"mp4"}
set targFolder to ((path to music folder as Unicode text) & "iTunes:iTunes Music:") as alias
set Filename to "Video List2" --Name the file
set target_file to (path to desktop as Unicode text) & Filename
set append_data to true --Set this to false if you want to overwrite, true if you want to add text to an existing file this has to be true for the repeat, otherwise you end up with just the last name.
set parameterString to makeSearchParameter()
set vidList to paragraphs of (do shell script "mdfind -onlyin " & quoted form of POSIX path of targFolder & space & parameterString)
set vidNameList to {}
set {TID, text item delimiters} to {text item delimiters, "/"}
repeat with i in vidList --new
set end of vidNameList to last text item of contents of i
end repeat --new
set text item delimiters to return
set vidNameList to vidNameList as text
set text item delimiters to TID
try --This part writes the file
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write vidNameList to the open_target_file starting at eof --new
close access the open_target_file
on error
try
close access file target_file
end try
end try
display dialog "I have finished making the video list"
on makeSearchParameter()
set paramString to "'kMDItemFSName = *."
set nextParam to " || kMDItemFSName = *."
if (count extension_list) is 1 then return paramString & (extension_list as text) & "'"
repeat with i in rest of extension_list
set paramString to paramString & " || kMDItemFSName = *." & i
end repeat
return paramString & "'"
end makeSearchParameter
Stefan’s point is a very good one. Searching for something (or a family of somethings) in the Finder should always be your last resort. Using mdls to get the metadata attributes of a sample of what you’re looking for, followed by mdfind to find them all is blindingly fast by comparison. mdfind is searching spotlight’s metadata database, not your directories. To learn more about these tools, you might read these tutorials which were written for Tiger but should work in Leopard:
I don’t get to script too often, so when a situation like this presents itself, I write my scripts with both options so I can have it for the future, if I need to reference it.
For example this is how I have done that line in my script
set vidNameList to reverse of vidNameList as text --set vidNameList to vidNameList as text -- for z-a list