Labeling files from an OmniOutliner file

Hi,
I have an OmniOutliner file with each line that is a different combination of letters/numbers. The total string is about 10 characters long for each line, but only the first 7 digits matter. I have three file folders on my Mac that files where these names (from the lines in the OmniOutliner document - which could really be transferred to any file format if needed) could possibly be in. Is there an AppleScript or a way to scan the OmniOutliner file & label any file with a matching name in the folders to the file? I would only need it to scan & match the first 7 digits of the line (actually, would prefer if it did only scan the first 7 digits).

Thanks!

  • Jesse

I don’t have omni outliner so I don’t really know how to get the text from its files, so the first thing you need to do is look at its applescript dictionary. But a generic way that works with a lot of applications is as follows:

set theFile to choose file
tell application “Omni Outliner”
open theFile
set theText to contents of document 1 → or content or text etc.
close document 1
end

– now you can do what you want with the text such as make it into a list so your can iterate through it
set text_as_list to paragraphs of theText → this separates it into its lines

– go through each line
repeat with i from 1 to count of text_as_list
set the_first_seven to text 1 thru 7 of (item i of text_as_list)
→ search the folder full of the files and check their names against the_first_seven
→ if a file match is found then set the label color of it
end repeat

I’m not incredibly handy with AppleScript, so a few of the things you said went over my head. I know the basics, but when it comes to coding - it is a pretty slow street for me.

That being said, if I broke the OmniOutliner data into a regular text document, with line breaks - would it work with that?

If you could - can you write out exactly what the AppleScript should look like?

Thanks,

  • Jesse

Ahhh, that’s why we’re here”to help you learn. I gave you a good outline to work with, now you just need to do some learning.

Yes, that makes it much easier and eliminates the need to use OmniOutliner all together. Then to read in the text file is simple because applescript can do that itself. I haven’t tried this but here’s what I think would work. Then you just need to try it and if something isn’t working right then do some searching to see if you can fix it. I added comments into the script so you can see what each part of the script does. That will help you search.

But maybe this will work right the first time… you never know… sometimes I get lucky. :slight_smile: Make sure to set your particular variables at the top of the script.

-- setup your intial variables such as the 3 folders and the text file
set folder1 to (path to desktop folder as Unicode text) & "testFolder1:"
set folder2 to (path to desktop folder as Unicode text) & "testFolder2:"
set folder3 to (path to desktop folder as Unicode text) & "testFolder3:"
set text_file to (path to desktop folder as Unicode text) & "textFile.txt"

-- get all of the files in those 3 folders 
tell application "Finder"
	set folder1_files to every file of entire contents of folder folder1
	set folder2_files to every file of entire contents of folder folder2
	set folder3_files to every file of entire contents of folder folder3
end tell

-- now make the folder's files into one big list so you can compare them against the text file
set all_folder_files to folder1_files & folder2_files & folder3_files

-- read in the text file
set theText to read theFile

-- turn the text into a list
set text_list to paragraphs of theText

-- this part is a little complicated because we have a repeat loop inside a repeat loop
-- but we need that because we have to repeat through each text list item and for each of those repeat through the folder files list to see if we have a match
repeat with i from 1 to count of text_list
	set the_first_seven to text 1 thru 7 of (item i of text_list)
	-- repeat through each file in the folder list to see if there is a match
	repeat with j from 1 to (count of all_folder_files)
		set this_folder_file to (item j of all_folder_files)
		
		-- get the name of the folder_file and compare it to the_first_seven
		tell application "Finder" to set fileName to name of this_folder_file
		if fileName contains the_first_seven then -- if we have a match then change the file's color
			-- label colors --0 none, 1 orange, 2 red, 3 yellow, 4 blue, 5 purple, 6 green, 7 grey
			tell application "Finder"
				set label index of this_folder_file to 4
			end tell
			exit repeat --> we found a match so we can exit the inner repeat loop
		end if
	end repeat
end repeat

Awesome, awesome. Thanks so much for the reply.

Your script had a few hiccups, which I fixed - but there’s one I may need your help on.

It seems that the line

set the_first_seven to text 1 thru 7 of (item i of text_list)

is trying to find those first 7 characters in the first digit of the line, instead of the entire line.

I’m getting this error:

Can you think of anything to fix it?

Thanks!

  • Jesse

I’m glad you’re getting it to work. I can think of 2 reasons why you’re getting that error. 1) there’s something wrong with that command or 2) there’s something wrong with the file. So let’s look at each individually.

For #1, try this. Here’s 2 methods to get the first 7 characters of a line. Try the first way and see if you get an error. If so then try the second. If only the 2nd way works then we fixed the problem. If the first way works then the command is not your problem.

set theText to "1234567890"

set the_first_seven to text 1 thru 7 of theText

--set the_first_seven to (text items 1 thru 7 of theText) as Unicode text

So if the command itself is not the problem then it must be the file. I assumed that every line had at least 7 characters to it, but maybe it doesn’t. Run this and look at the results to check each list item. Maybe you will spot the problem with the file.

-- setup your intial variables such as the 3 folders and the text file
set folder1 to (path to desktop folder as Unicode text) & "testFolder1:"
set folder2 to (path to desktop folder as Unicode text) & "testFolder2:"
set folder3 to (path to desktop folder as Unicode text) & "testFolder3:"
set text_file to (path to desktop folder as Unicode text) & "textFile.txt"

-- get all of the files in those 3 folders 
tell application "Finder"
	set folder1_files to every file of entire contents of folder folder1
	set folder2_files to every file of entire contents of folder folder2
	set folder3_files to every file of entire contents of folder folder3
end tell

-- now make the folder's files into one big list so you can compare them against the text file
set all_folder_files to folder1_files & folder2_files & folder3_files

-- read in the text file
set theText to read theFile

-- turn the text into a list
set text_list to paragraphs of theText

return text_list

If you want to allow some lines to have less than 7 characters then we can just ignore those lines using an if statement (or an error statement) like so:

repeat with i from 1 to count of text_list
	
	if (count of (item 1 of text_list)) ≥ 7 then --> check to make sure we have at least 7 characters
		
		set the_first_seven to text 1 thru 7 of (item i of text_list)
		-- repeat through each file in the folder list to see if there is a match
		repeat with j from 1 to (count of all_folder_files)
			set this_folder_file to (item j of all_folder_files)
			
			-- get the name of the folder_file and compare it to the_first_seven
			tell application "Finder" to set fileName to name of this_folder_file
			if fileName contains the_first_seven then -- if we have a match then change the file's color
				-- label colors --0 none, 1 orange, 2 red, 3 yellow, 4 blue, 5 purple, 6 green, 7 grey
				tell application "Finder"
					set label index of this_folder_file to 4
				end tell
				exit repeat --> we found a match so we can exit the inner repeat loop
			end if
		end repeat
	end if
end repeat

Awesome! Your responses have been a lot of help, and have saved me a lot of petty work myself. Thanks so much for all your help in this endeavor.

It turned out that I just needed to replace in your original script, this;


-- read in the text file
read file text_file

-- turn the text into a list
set text_list to paragraphs of result