Hi everyone,
I’m running an exiftool command in Terminal to generate a list of photoshop files with all their respective layer names.
The data that is returned gets dumped into a text document on my Desktop but I need to process this text a little further so that eventually I can paste a simple string of filenames, separated by commas, into Lightroom when I perform a search for filenames.
So far I have been running multiple find/replace searches in TextWrangler to clean up the text for my needs then I found out that I could consolidate several individual searches into a single AppleScript and be done with one click.
In that regard this is really more of a TextWrangler, grep question than it is an AppleScript question, but I know this a solid pool of knowledge and someone here problems knows this answer.
Currently, when I run my AppleScript I’m left with multiple lines of filenames. Most of those lines have commas as the end, which I want to keep. But for the few lines that do not end with commas, how can I do a replacement that is essentially: “for any lines that do not end with a comma — insert a comma”?
As an example, let’s say I’m left with a text doc that looks like this:
854A9142,854A9144,854A9143,854A9142
854A9589,
854A9607,
854A9850,
854A9851,
854A9852,
854A9854,
EE8A8457,EE8A8466,EE8A8464,EE8A8462,EE8A8459,EE8A8457
EE8A8760,
EE8A8764,
EE8A8769,
EE8A8774,
EE8A8775,
EE8A8776,
EE8A8842,EE8A8861,EE8A8859,EE8A8857,EE8A8855,EE8A8852,EE8A8850,EE8A8848,EE8A8842
…those three long lines do not have commas at the end and ultimately everything needs to be comma separated. So my original goal was to simply add commas to the end of any lines that don’t have one then removing the line breaks.
I should add that I already have a “workaround” that may be satisfactory. I can do a find/replace for all line breaks, replacing the breaks with commas. Then I end up with several sets of double commas, but one more search for , replacing with , fixes that. Perhaps this is a viable solution.
However, even if this last little “trick” works I’m still left with a single comma at the very beginning of the string. In TextWrangler I can remove that comma with a search for ^. replacing with nothing.
But…I can’t get that same grep command to run in AppleScript. I realize I need to insert \ to get grep commands to work in AppleScript but evidently I can’t get the correct syntax in AppleScript to remove that very first character, a comma.
This workaround changes the subject line of my post. I suppose it’s either: how to insert a comma at the end of lines without one? Or…how do I remove the very first character of a line in TextWrangler, using AppleScript / grep syntax?
Thanks in advance!
[edited to add:] I should probably add a snippet of my AppleScript so far. This one gets around the problem with the missing commas at the ends of some lines by doing my trick: "replace line breaks with comma’ then “replace , with ,” but if this is going to be my solution I would still like to remove that initial character, a comma, from the resulting string. (There are folder paths, specific to my system, that I’ve “redacted” here.)
tell application "TextWrangler"
set s_options to {starting at top:true}
replace "SourceFile,LayerNames" using "" searching in text 1 of active document of text window 1 options s_options
replace "***[folder path 1]***" using "" searching in text 1 of active document of text window 1 options s_options
replace "***[folder path 2]***" using "" searching in text 1 of active document of text window 1 options s_options
replace "Background, Background copy" using "" searching in text 1 of active document of text window 1 options s_options
replace ", Layer 1" using "" searching in text 1 of active document of text window 1 options s_options
replace ".psd" using "" searching in text 1 of active document of text window 1 options s_options
replace ".CR3" using "" searching in text 1 of active document of text window 1 options s_options
replace "\"" using "" searching in text 1 of active document of text window 1 options s_options
replace "\\r" using "," searching in text 1 of active document of text window 1 options s_options
replace ",," using "," searching in text 1 of active document of text window 1 options s_options
replace " " using "" searching in text 1 of active document of text window 1 options s_options
end tell