Re: Using 'Repeat'
Re: Using 'Repeat'
- Subject: Re: Using 'Repeat'
- From: Nigel Garvey <email@hidden>
- Date: Sun, 25 Mar 2001 18:07:32 +0100
email@hidden wrote on Sun, 25 Mar 2001 17:12:55 +1000:
>
Hi All,
>
>
I am not too clear on using the repeat capabilities in Applescript. The
>
following script give me the following error: "Finder got an error: Can't
>
make file "B3761111.123" into a item".
>
>
What I am trying to do is move any number of files with similar names to a
>
new folder.
>
>
tell application "Finder"
>
set test_folder to "Macintosh HD:test folder:"
>
set test_folder2 to "Macintosh HD:test folder2:"
>
set file_list to list folder test_folder
>
set search_term to "376"
>
repeat with an_item in file_list
>
if an_item contains search_term then
>
move file an_item to test_folder2
>
end if
>
end repeat
>
end tell
There's nothing wrong with your repeat loop, but there are some issues
with the 'move' line.
Firstly, with this sort of repeat structure, the value of 'an_item' is a
reference to an item in 'file_list', not the value of the item itself.
This is "dereferenced" to the item's value for the comparison in the
preceding line, but can't itself be used with 'file' here. (This is the
cause of your error message.) You need to dereference it explicitly with
'contents of an_item'.
Secondly, 'list folder' only give the names of the items in a folder.
Before you use 'move' on one of these names, you'll need to supply the
path to the folder itself. This is easy in your script as you already
have it in the variable 'test_folder'.
Thirdly, you need to use the word 'folder' before the folder names so
that the Finder knows what you mean. (However, this may not be necessary
if you have Jon's Commands installed.)
So (watch out for line wrap):
set test_folder to "Macintosh HD:test folder:"
set test_folder2 to "Macintosh HD:test folder2:"
set file_list to list folder test_folder
set search_term to "376"
repeat with an_item in file_list
if an_item contains search_term then
tell application "Finder"
move file (contents of an_item) of folder test_folder to folder
test_folder2
end tell
end if
end repeat
This assumes that only file names will include the string "376". If the
source folder contains any subfolders with "376" in their names, these
too will be selected by the 'if' line and will cause an error when
associated with the word 'file' in the 'move' line. If this is a
possibility, you should change the word 'file' to 'item'.
Having noted all this for completeness in answering your query, I should
also add that the same result can be obtained more simply and more
quickly by using a Finder 'whose' expression instead of a repeat:
set test_folder to "Macintosh HD:test folder:"
set test_folder2 to "Macintosh HD:test folder2:"
set search_term to "376"
tell application "Finder"
move every file of folder test_folder whose name contains search_item
to folder test_folder2
end tell
NG