PDA

View Full Version : Applescript to label files from a queried list




The Butsmeister
Jan 17, 2008, 07:30 AM
Hi,

Does anyone know of a way to label files on OSX using a text file as a reference.
So I have a text file with a list of Mac files that I want to query and get the Finder to label any files that exist in a designated folder on the Mac BUT only if they appear in the referenced text file.

thanks



hhas
Jan 17, 2008, 08:01 AM
General example:

set theFolder to alias "MacHD:Users:Foo:foldername:"
set fileNames to {"filename1", "filename2"}
tell application "Finder"
set label index of (every item whose name is in fileNames) of theFolder to 1
end tell

If you want more specific advice you'll need to provide a sample of the text file's contents.

The Butsmeister
Jan 17, 2008, 08:19 AM
Thanks for your response hhas.
I've attached a screenshot to show what I'm trying to achieve.
Basically I want to label any eps file in the Images2 folder to red if the name exists in the text file called imagename. So in this instance the only eps image not to get labelled is BLMAH096P.eps
I guess I need to query each line of text within the text file to see if it matches an image name but I'm not sure how to do this
Many thanks

lancestraz
Jan 17, 2008, 12:16 PM
See if this works.
try
set theFolder to (choose folder with prompt "Choose a folder.")
set textFile to (choose file with prompt "Choose a text file." without invisibles) as file specification

open for access textFile with write permission
set fileNames to (read textFile using delimiter "
")
close access textFile

tell application "Finder"
set label index of (every item whose name is in fileNames) of theFolder to 2
end tell
on error theError
display dialog theError buttons {"OK"} default button 1
end try

hhas
Jan 17, 2008, 04:13 PM
See if this works.
...

This can be simplified a little, plus I'd recommend avoiding the 'read' command's 'using delimiter' parameter and splitting the text using 'every paragraph of...' as it's more reliable. Depending on the text file's encoding (primary, UTF8, UTF16), you may also need to specify 'string', '«class utf8»' or 'Unicode text' for the 'read' command's 'as' parameter.

try
set theFolder to (choose folder with prompt "Choose a folder.")
set textFile to (choose file with prompt "Choose a text file." without invisibles)

set fileNames to every paragraph of (read textFile as string)

tell application "Finder"
set label index of (every item whose name is in fileNames) of theFolder to 2
end tell
on error theError
display dialog theError buttons {"OK"} default button 1
end try

HTH

lancestraz
Jan 17, 2008, 04:33 PM
This can be simplified a little, plus I'd recommend avoiding the 'read' command's 'using delimiter' parameter and splitting the text using 'every paragraph of...' as it's more reliable. Depending on the text file's encoding (primary, UTF8, UTF16), you may also need to specify 'string', '«class utf8»' or 'Unicode text' for the 'read' command's 'as' parameter.

try
set theFolder to (choose folder with prompt "Choose a folder.")
set textFile to (choose file with prompt "Choose a text file." without invisibles)

set fileNames to every paragraph of (read textFile as string)

tell application "Finder"
set label index of (every item whose name is in fileNames) of theFolder to 2
end tell
on error theError
display dialog theError buttons {"OK"} default button 1
end try

HTH
Oh cool, I didn't know you could do that! That's much better. :)

The Butsmeister
Jan 18, 2008, 04:34 AM
Unfortunately can't get this to work for me, get the following error shown in attachment

The Butsmeister
Jan 18, 2008, 05:58 AM
Has anyone got any ideas what the previous finder error message means in respect of the script?

hhas
Jan 18, 2008, 05:58 AM
Unfortunately can't get this to work for me, get the following error shown in attachment

Your file is saved in rich text format (.rtf). Either:

1. Resave it as a plain text (.txt) file. I'd suggest using the UTF8 encoding, and modifying the 'read as string' command in the previous script to 'read as «utf8»'.

Or, if that's not an option:

2. Have AppleScript open the file in TextEdit and get the text from from there:

try
set theFolder to (choose folder with prompt "Choose a folder.")
set textFile to (choose file with prompt "Choose a text file." without invisibles)

tell application "TextEdit"
open textFile
set fileNames to every paragraph of text of document 1
close document 1
end tell

tell application "Finder"
set label index of (every item whose name is in fileNames) of theFolder to 2
end tell
on error theError
display dialog theError buttons {"OK"} default button 1
end try

The Butsmeister
Jan 18, 2008, 06:52 AM
Hmm, now I get a different message after trying both methods HHas suggested . . . sorry to be a pain, but I'm missing something here

The Butsmeister
Jan 18, 2008, 07:42 AM
Hi all,
Thanks hhas in helping me with this.
Now works fine using utf8 and making sure that hidden file extension in image name is included in text file.

The Butsmeister
Jan 18, 2008, 08:16 AM
Just a further question on this script.
How do I specify if I want to search for filenames in a subfolder so that rather than looking into the designated folder, I can search through all folders within this folder?

thanks

The Butsmeister
Jan 18, 2008, 09:32 AM
Answered my own question here . . . but thought I would post it for reference

set label index of (every item whose name is in fileNames) of entire contents of theFolder

thighofjustice
May 12, 2008, 10:04 AM
I found this thread, and it fits perfectly with something I am working on..

it works great, but is there a way to have the filenames to be searched with "contains name", instead of the exact name?

example.

if I have a list of numbers:
2820052
2846645
2265688

That correspond to files:
2820052_chb.tif
2846645_acr_s.tif
2265688_hho_.tif

and I want the files to be marked, can I search for filenames contain numbers in txt file, instead of having to add the "_chb.tif" to every number in the list. I am going to be searching about a terabyte or more of images.

something like:

tell application "Finder"
set label index of (every item whose name contents is not in fileNames) of entire contents of theFolder to 2




Thanks for the help.

Jensend
Dec 19, 2008, 06:15 PM
How would I modify this script so it only checks the 4rd through 9th character of the file name against the text list?