Hey folks. Got a tiny conundrum that I’m sure I’m overlooking something simple. I recently worked a lot with a few people here on a Canon DPP processing script: page2
http://macscripter.net/viewtopic.php?id=42323
and I have been tweaking/tightening up the file handling portion of the script. Namely:
on process_item(this_item)
set the item_info to info for this_item
set nam to the name of the item_info
set dir to the POSIX path of (this_item as alias)
if nam contains "tif" then
if nam contains "_y" or nam contains "_t" or nam contains "_a" or nam contains "_c" then
tell application "Finder"
set jpgFolder to POSIX path of ((this_item as text) & "::") & "JPEG"
do shell script "/bin/mkdir -p " & quoted form of jpgFolder
The part I am working on is making the “if nam contains” more elegant and functional for my purposes. I want to be able to have a list of things that “if exists in the filename” that I can edit more easily for different output purposes and filenames.
EX: if the nam contains _y, _c, _t, _a, _z7 etc etc. can get very long when I have a list of 15 or so items in it.
I am trying to get
set nameText to {"_y", "_t", "_a", "_c1.", "_c2.", "_c3.", "_c7.", "_c8.", "_c9.", "_c10.", "_c11.", "_c12."}
if nam contains nameText then
to work, but I know I’m missing something here. I feel like it would be more like “If nam contains ‘something’ in nameText then” but I guess I’m still pretty novice and searching around I don’t exactly know what I am looking for is called. Anybody have a second to help a bruv out?
Since you can have multiple delimiters in AppleScript I would use multipel delimiters and simply count if the umber of text items are bigger than 1.
set nameText to {"_y", "_t", "_a", "_c1.", "_c2.", "_c3.", "_c7.", "_c8.", "_c9.", "_c10.", "_c11.", "_c12."}
set {oldTID, appleScript's text item delimiters} to {AppleScript's text item delimiters, nameText}
set namContainsNameText to number of text items of nam > 1
set AppleScript's text item delimiters to oldTID
if namContainsNameText then
p.s. not behind a mac right now so I haven’t tested the code
@DJ hmm. I’m not sure I understand. Maybe I didn’t word my problem the way I think I did? I need to check the filename “nam” to see if it contains ANY of the things in the list “nameText”. If it does, it tells photoshop to make a jpg, if it doesn’t then it makes a tif. Are you saying to change the delimiters to the items in the list? I guess I don’t see how that helps.
I just re-read this. Lemme see if I get this right. You are suggesting I change the text delimiters to equal the list of stuff I’m searching for in the filename, then saying “if” the filename (which includes delimiters) ever equal one of those “then”. Do I have that right?
I guess I just assumed there was an easier way to call out a search on a filename based on a list that my newbie brain doesn’t know about. Is there any reason why your method would interfere with anything else going on inside this script (which gets run as a droplet)?
Maybe some examples makes it clear:
set fileName to "some_file_name.jpg"
set fileNameMatches to fileName contains "_a" or fileName contains "_b" or fileName contains "_c" or fileName contains "_e" or fileName contains "_f"
--fileNameMatches = true
When you have 15 or more expressions your code does start to become looking ugly. So you’re looking for a way that the code keeps more readable and maybe more dynamic (if the items are variable). when using text item delimiters and there is no match with one of the delimiters it means the string only has 1 text item. If there is one or more matches with the delimiters the string contains multiple text items. So after the delimiters are set we only need to count text items. The code below is simpler written down as the code above (a statement that is of course arguable) but does the same job.
set fileName to "some_file_name.jpg"
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"_a", "_b", "_c", "_d", "_e", "_f"}}
set fileNameMatches to number of text items of fileName > 1
set AppleScript's text item delimiters to oldTID
--fileNameMatches = true
Great! I think I get what you mean now. I will work on it tomorrow and let you know what I come up with. Thanks!
@DJ - Success! Once I actually typed the code in context I understood exactly what you meant. What a neat little trick! I haven’t had much cause to mess with delimiters but it seems like this was a perfect example for me to learn with. I also used this method in another script I am working on to help me move certain files around and it works like a charm. Dank je wel!
-Jeff