Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

The Butsmeister

macrumors newbie
Original poster
Jan 17, 2008
9
0
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

macrumors regular
Oct 15, 2007
126
0
General example:

Code:
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

macrumors newbie
Original poster
Jan 17, 2008
9
0
RE: Applescript to label files from a queried list

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
 

Attachments

  • Picture 1.png
    Picture 1.png
    73.4 KB · Views: 162

lancestraz

macrumors 6502a
Nov 27, 2005
898
0
RI
See if this works.
Code:
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

macrumors regular
Oct 15, 2007
126
0
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.

Code:
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

macrumors 6502a
Nov 27, 2005
898
0
RI
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.

Code:
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

macrumors newbie
Original poster
Jan 17, 2008
9
0
RE: Applescript to label files from a queried list

Unfortunately can't get this to work for me, get the following error shown in attachment
 

Attachments

  • Picture 2.png
    Picture 2.png
    43.7 KB · Views: 63

The Butsmeister

macrumors newbie
Original poster
Jan 17, 2008
9
0
RE: Applescript to label files from a queried list

Has anyone got any ideas what the previous finder error message means in respect of the script?
 

hhas

macrumors regular
Oct 15, 2007
126
0
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:

Code:
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

macrumors newbie
Original poster
Jan 17, 2008
9
0
RE: Applescript to label files from a queried list

Hmm, now I get a different message after trying both methods HHas suggested . . . sorry to be a pain, but I'm missing something here
 

Attachments

  • Picture 3.png
    Picture 3.png
    15.8 KB · Views: 78

The Butsmeister

macrumors newbie
Original poster
Jan 17, 2008
9
0
RE: Applescript to label files from a queried list

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

macrumors newbie
Original poster
Jan 17, 2008
9
0
rE: Applescript to label files from a queried list

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

macrumors newbie
Original poster
Jan 17, 2008
9
0
re: Applescript to label files from a queried list

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

macrumors newbie
May 12, 2008
4
0
A question regarding this thread.

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:

Code:
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

macrumors 65816
Dec 19, 2008
1,452
1,666
How would I modify this script so it only checks the 4rd through 9th character of the file name against the text list?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.