This is a bit of an extension of the prior script I was working on. And actually this script I’m about to post works fine, other than “Choose File” dialog boxes NOT coming to the front as I would like. But here’s what’s going on:
I will be handed a list that looks like this.
3
003_image.jpg
9/3/12 7/14/12 @ 07:38:27
6
006_image.jpg
9/3/12 7/14/12 @ 10:01:49
9
009_image.jpg
9/3/12 7/14/12 @ 08:08:09
I’m opening this in TextWrangler and using grep searches to refine the list because as-is, it has more information than I need. All I really want is the file name “003_image.jpg” bits. I’ve got two grep searches that tidy my list. One line takes care of all the extra junk, one takes care of the extra line breaks that occur. Like this:
tell application "TextWrangler"
activate
open find window
replace "^\\d{3}\\s|^\\d{2}\\s|^\\d{1}\\s|^\\d{1}/\\d{1}/\\d{2}.*|^\\d{2}/\\d{2}/\\d{2}.*|^\\d{1}/\\d{2}/\\d{2}.*|^\\d{2}/\\d{1}/\\d{2}.*" using "" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
replace "\\r\\r" using "\\r" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
close find window saving no
end tell
So far, so good. This modifies my list to now look like this:
003_image.jpg
006_image.jpg
009_image.jpg
I want to use the above list to “look into a folder” whose contents looks like this:
001_image.jpg
002_image.jpg
003_image.jpg
004_image.jpg
005_image.jpg
006_image.jpg
007_image.jpg
008_image.jpg
009_image.jpg
010_image.jpg
If a file is on the list, it needs to get labeled. This is where my other script chunk comes into play. I’m using System Events to Copy the TextWrangler list to the clipboard, then using the clipboard contents to select and label the files in question:
set theFolder to (choose folder with prompt "Choose a Folder")
set fileNames to paragraphs of (the clipboard)
tell application "Finder"
set label index of (every item whose name is in fileNames) of theFolder to 1
end tell
So ultimately, I want to combine these two major steps:
¢ Refine the list down (and copy contents to clipboard)
¢ Label files whose names reside in the clipboard
In searching the web last night for grep + TextWranlger + Applescript options I wandered onto a Droplet scenario that is almost what I need. It at least prompts for the text file (holding the list), refines the list and copies the list to the clipboard.
on run
tell me to set theFiles to choose file "Please choose files:" with multiple selections allowed
cleanFiles(theFiles)
end run
on open theFiles
cleanFiles(theFiles)
end open
on cleanFiles(theFiles)
tell application "TextWrangler"
repeat with myFile in theFiles
open myFile opening in new_window
tell window 1
replace "^\\d{3}\\s|^\\d{2}\\s|^\\d{1}\\s|^\\d{1}/\\d{1}/\\d{2}.*|^\\d{2}/\\d{2}/\\d{2}.*|^\\d{1}/\\d{2}/\\d{2}.*|^\\d{2}/\\d{1}/\\d{2}.*" using "" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
replace "\\r\\r" using "\\r" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
# run unix filter "~/Library/Application Support/TextWrangler/Unix Support/Unix Filters/myFilter.pl"
tell application "TextWrangler" to activate
tell application "System Events"
tell process "TextWrangler"
keystroke "a" using command down
keystroke "c" using command down
key code 126
end tell
end tell
close saving yes
end tell
end repeat
end tell
end cleanFiles
This works a-okay! Now I’m trying to append the file labeling function to the script. It does indeed work, right now! However, my “Choose FIle” and “Choose Folder” dialog boxes are not coming to the front.
So here’s final combined script with the only issue being the “Choose” windows:
on run
tell me to set theFiles to choose file "Please choose files:" with multiple selections allowed
cleanFiles(theFiles)
end run
on open theFiles
cleanFiles(theFiles)
end open
on cleanFiles(theFiles)
tell application "TextWrangler"
repeat with myFile in theFiles
open myFile opening in new_window
tell window 1
replace "^\\d{3}\\s|^\\d{2}\\s|^\\d{1}\\s|^\\d{1}/\\d{1}/\\d{2}.*|^\\d{2}/\\d{2}/\\d{2}.*|^\\d{1}/\\d{2}/\\d{2}.*|^\\d{2}/\\d{1}/\\d{2}.*" using "" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
replace "\\r\\r" using "\\r" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
# run unix filter "~/Library/Application Support/TextWrangler/Unix Support/Unix Filters/myFilter.pl"
tell application "TextWrangler" to activate
tell application "System Events"
tell process "TextWrangler"
keystroke "a" using command down
keystroke "c" using command down
key code 126
end tell
end tell
close saving yes
end tell
end repeat
end tell
set theFolder to (choose folder with prompt "Choose a Folder")
set fileNames to paragraphs of (the clipboard)
tell application "Finder"
set label index of (every item whose name is in fileNames) of theFolder to 1
open theFolder
end tell
end cleanFiles
What am I missing here? I know I am really close to a clean solution. Just a few minor details to tidy this up nicely.
Thanks so much for any input here!