Can't use selected files in script

Hi everybody,

I’m a newbie to AppleScript and this is my first post. I have a seemingly simple problem but can’t figure it out. I’m trying to append text to file comments like this:

tell application "Finder"
	set source_folder to choose folder with prompt "Choose your folder"
	set itemList to entire contents of source_folder
	set myComment to text returned of (display dialog "Enter comment text" default answer "&tag")
	repeat with myItem in itemList
		set origComment to comment of myItem
		set the comment of myItem to origComment & " " & myComment
	end repeat
end tell

Can someone show me how to get this to work with the currently selected file/folder instead of prompting for it?

I’ve tried several attempts. Sometimes I can even get a property of the selected file to show, but it won’t ever carry through to the rest of the script.

Thanks a bunch for your help!

Model: MacBook Pro
AppleScript: 1.10.7
Browser: Firefox 2.0.0.11
Operating System: Mac OS X (10.5)

Nova:

You almost had it. You still need to specify that you want every file:

tell application "Finder"
	set source_folder to choose folder with prompt "Choose your folder"
	set itemList to every file of entire contents of source_folder--Selects all files 
	set myComment to text returned of (display dialog "Enter comment text" default answer "&tag")
	repeat with myItem in itemList
		set origComment to comment of myItem
		set the comment of myItem to origComment & " " & myComment
	end repeat
end tell

Keep in mind that this script will affect every file in every sub-folder of the chosen folder as well. If that is not your intent, just remove the of entire contents portion of the set itemList line.

Good luck,

I think the original question wanted to avoid using choose folder, not refine the file selection after the folder is chosen.

You can do this by getting the current Finder selection, like this:

tell application "Finder"
	set theSelection to the selection
	set itemList to entire contents of item 1 of theSelection
	set myComment to text returned of (display dialog "Enter comment text" default answer "&tag")
	repeat with myItem in itemList
		set origComment to comment of myItem
		set the comment of myItem to origComment & " " & myComment
	end repeat
end tell

Keep in mind, though, that the selection is a LIST. So it’s possible you could have more than 1 item selected, and this script doesn’t handle that possibility, it only processes the first item selected.

In order to be certain you act on the entire selection, you will need to loop through theSelection, then loop through itemList.

Was this what you wanted?

Thanks for your responses CAS and Nitewing!

My original question was how to get the script to work on the selected files. I was also intending to apply the comment to every folder or file within and below the level of that selected, so thanks to CAS for pointing our my oversight.

I’ll have to play around with these when I wake up tomorrow. Right now, Nitewing, I’m wondering why you used item_1 of the selection instead of leaving out that bit and just using entire contents. Also, I’m curious how I would construct a script that would loop through and catch everything like you said I’d need to do to tag every file.

By the way, thanks for posting those example files on your website, Nitewing. The Append-Comment was what got me started. :slight_smile:

Thanks again,
Nova

Hi Nova,

I guess you mean this


tell application "Finder"
	set myComment to text returned of (display dialog "Enter comment text" default answer "&tag")
	repeat with oneItem in (get selection)
		set origComment to comment of oneItem
		if origComment is not "" then set myComment to origComment & " " & myComment
		set comment of oneItem to myComment
	end repeat
end tell

Thanks StefanK, but that doesn’t seem to get all the subfolders too like I’m hoping. I tried running your script with the subfolder and files selected and it seemed to put a duplicate of every tag in every comment.

In my latest attempt, I’m trying to loop through every item and compile a list like Nitewing mentioned. But, it tells me that it can’t make the following “every item” insertion into type reference.


on compile_List(theSelection)
	tell application "Finder"
		set itemList to every item of theSelection

Can someone point out what’s wrong or show me a way to get every file in every subfolder? Here’s the script I’m using:

property mySelection : get selection

enter_Comment("&tag")
compile_List(mySelection)
tag_Items(myComment)

--Prompt for new comment text
on enter_Comment(defaultText)
	tell application "Finder"
		set myComment to text returned of (display dialog "Enter comment text" default answer defaultText)
	end tell
end enter_Comment

--List all contents of selection
on compile_List(theSelection)
	tell application "Finder"
		set itemList to every item of theSelection
		repeat with i in theSelection
			if itemList is (empty) then set itemList to entire contents of i
			set end of itemList to entire contents of i
		end repeat
	end tell
end compile_List

--Append new comment
on tag_Items(newComment)
	tell application "Finder"
		repeat with oneItem in itemList
			set origComment to comment of oneItem
			if origComment is not "" then set newComment to origComment & " " & newComment
			set comments of oneItem to newComment
		end repeat
	end tell
end tag_Items

I hope adding the handlers isn’t messing things up.

Thanks again

Ok, then I missunderstood it.

This script changes all files of the selection including all files of the subfolders, if any selected item is a folder


property newComment : ""

tell application "Finder"
	set newComment to text returned of (display dialog "Enter comment text" default answer "&tag")
	set sel to selection
end tell
repeat with oneItem in sel
	set {folder:Fo, package folder:Pa} to info for oneItem as alias
	if Fo and not Pa then
		tell application "Finder" to set subFiles to files of entire contents of oneItem
		tag_Items(subFiles)
	else
		tag_Items({contents of oneItem})
	end if
end repeat


on tag_Items(itemList)
	tell application "Finder"
		repeat with oneItem in itemList
			set origComment to comment of oneItem
			if origComment is not "" then set newComment to origComment & " " & newComment
			set comment of oneItem to newComment
		end repeat
	end tell
end tag_Items

I used item_1 just because I hadn’t time to write proper loop to handle all items, that’s why I mentioned that you’d need to do that. Sorry if it wasn’t clear!

Thanks! Good to know that those items are doing their job, stimulating new scripters! :slight_smile:

Thanks for your last post StefanK. It seems though that newComment never resets itself, so the last comment ends up being a complete string of all previous comments.

I’m assuming that making newComment a property kept it from reseting with each loop. I just reset the comment before every loop and it works great. Here’s the script in case someone else comes along and wants to use it:


property myComment : ""

tell application "Finder"
	set myComment to text returned of (display dialog "Enter comment text" default answer "&tag")
	set sel to selection
end tell
repeat with oneItem in sel
	set {folder:Fo, package folder:Pa} to info for oneItem as alias
	if Fo and not Pa then
		tell application "Finder" to set subFiles to files of entire contents of oneItem
		tag_Items(subFiles)
	else
		tag_Items({contents of oneItem})
	end if
end repeat


on tag_Items(itemList)
	tell application "Finder"
		repeat with oneItem in itemList
			set newComment to myComment
			set origComment to comment of oneItem
			if origComment is not "" then set newComment to origComment & " " & newComment
			set comment of oneItem to newComment
		end repeat
	end tell
end tag_Items

Thanks a lot everyone

I thought I’d just provide the final copy of my script. Hopefully this doesn’t overdue this string of posts, but it’s nice to get some resolution and leave something clean behind for others.

This script will be very useful as I begin tagging my files and using metadata for my organization.

Cheers,
Nova


--This script appends a comment to the selected file/folder as well as every file and folder below it in the folder hierarchy

property myComment : ""

--Prompt for new comment text (parameter is default entry)
enter_Comment("&tag")

--Set selection
tell application "Finder"
	set mySelection to selection
end tell

--Tag selected file/folder
tag_Items(mySelection, myComment)

--Tag files
repeat with oneItem in mySelection
	set {folder:Fo, package folder:Pa} to info for oneItem as alias
	if Fo and not Pa then
		tell application "Finder" to set subFiles to entire contents of oneItem
		tag_Items(subFiles, myComment)
	else
		tag_Items({contents of oneItem}, myComment)
	end if
end repeat

--Handler: Prompts for text (with supplied default entry)
on enter_Comment(defaultText)
	tell application "Finder"
		set myComment to text returned of (display dialog "Enter comment text" default answer defaultText)
	end tell
end enter_Comment

--Handler: Appends supplied text to comment of supplied items
on tag_Items(itemList, theComment)
	tell application "Finder"
		repeat with oneItem in itemList
			set newComment to theComment
			set origComment to comment of oneItem
			if origComment is not "" then set newComment to origComment & " " & newComment
			set comment of oneItem to newComment
		end repeat
	end tell
end tag_Items

Sorry :slight_smile:

This avoids the error


property myComment : ""

tell application "Finder"
	set myComment to text returned of (display dialog "Enter comment text" default answer "&tag")
	set sel to selection
end tell
repeat with oneItem in sel
	set {folder:Fo, package folder:Pa} to info for oneItem as alias
	if Fo and not Pa then
		tell application "Finder" to set subFiles to files of entire contents of oneItem
		tag_Items(subFiles)
	else
		tag_Items({contents of oneItem})
	end if
end repeat


on tag_Items(itemList)
	tell application "Finder"
		repeat with oneItem in itemList
			set origComment to comment of oneItem
			if origComment is "" then
				set comment of oneItem to myComment
			else
				set comment of oneItem to origComment & " " & myComment
			end if
		end repeat
	end tell
end tag_Items

Note: A property can be seen everywhere unless a normal (local) variable, which can only be seen in the scope where it was defined.