Variable for getting group name

I have the following scenario:

Several rtf files reside in a group containing the week number in the name.

Each of the rtf file should automatically have the name of the corresponding group at the top.

Is there any kind of variable/script which I could use for that?


error "The variable theDivider is not defined." number -2753 from "theDivider"

Do you have a file selected? This works out-of-the-box, just copy and pasting the code from here.

Exactly! Thanks a lot! It’s working really smooth now!

Would it also be possible to write that information in the files (first line of the RTFs)?

I’m sorry, but I haven’t tried writing Apple Scripts yet. Would it be possible that you post the code? That would be really great!

Thanks in advance!

Thanks Korm for this neat piece of code—it is surprising how many times I find such things here that I did not realize I needed! :laughing:

I tried this:

(* Prefix the items in the selection with my group name
by korm 20150227
https://discourse.devontechnologies.com/t/variable-for-getting-group-name/18354/1
*)

property theDivider : ": "

tell application id "DNtp"
	set theSelection to the selection
	set groupPrefix to (the name of the current group) & theDivider
	repeat with thisItem in theSelection
		if the type of thisItem is not "group" then
			if the name of thisItem does not contain groupPrefix then
				set the name of thisItem to groupPrefix & (the name of thisItem)
				open for access thisItem with write permission
				write groupPrefix to thisItem
				close access thisItem
			end if
		end if
	end repeat
end tell

But it doesn’t work.

I really appreciate that, honestly. I dug a little deeper and came up with this solution:

(* Prefix the items in the selection with my group name
by korm 20150227
https://discourse.devontechnologies.com/t/variable-for-getting-group-name/18354/1
*)

property theDivider : ": "

tell application id "DNtp"
	set theSelection to the selection
	set groupPrefix to (the name of the current group) & theDivider
	repeat with thisItem in theSelection
		if the type of thisItem is not "group" then
			if the name of thisItem does not contain groupPrefix then
				set the name of thisItem to groupPrefix & (the name of thisItem)
				if the type of thisItem is in {"rtf", "rtfd", "txt"} then
					set foo to (open for access thisItem with write permission)
					set theData to (read foo)
					write groupPrefix & theData to thisItem
				end if
			end if
		end if
	end repeat
end tell

While it’s already checking the file type correctly it doesn’t alter the file’s content at all…

I also tried this:

(* Prefix the items in the selection with my group name
by korm 20150227
https://discourse.devontechnologies.com/t/variable-for-getting-group-name/18354/1
*)

property theDivider : ": "

tell application id "DNtp"
	set theSelection to the selection
	if theSelection is {} then error "Select an item, please"
	set groupPrefix to (the name of the current group) & theDivider
	repeat with thisItem in theSelection
		if the type of thisItem is not "group" then
			if the name of thisItem does not contain groupPrefix then
				set the name of thisItem to groupPrefix & (the name of thisItem)
				if the type of thisItem is in {"rtf", "rtfd", "txt"} then
					set oldText to the rich text of thisItem
					set the rich text of thisItem to groupPrefix & oldText
				end if
			end if
		end if
	end repeat
end tell

…which seemed to be a simpler solution but to no avail.

Thanks a lot!

I just realized that it removed all the rich text formatting though.