-2

In my script I have strings like G014_1998_24.jpg or G024_2006_08.jpg. From these strings I need to extract the last consecutive digits via Applescript. So in my example strings I would need to retrieve the sequences 24 or 08 (the latter preferrably with the leading zero).

Is there any way to do this with an Applescript?

Edit: The strings could also look like this: G014_1998.jpg, G014_1998_A.jpg, G014_1998_AB.jpg etc. (in these cases the script should always retrieve 1998). So I am really looking for the last digit sequence and cannot rely on the position of these digits, as I cannot predict the positions.

  • They eventually will be in a loop. However, I will take care of the loop structure myself, that's not what I am having issues with. So I need to know how to extract the last digit sequence from a single string (that could take all of the above forms). – mdomino Jan 18 '18 at 10:22
1

Two possible methods:

Pure AppleScript

    set alphabet to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    set the text item delimiters of AppleScript to ¬
        {space} & characters of the alphabet & {".", "_"}
    set a to text items of "G024_2006_08.jpg" as text
    set b to text items of "G014_1998_AB.jpg" as text

    get last word of a --> returns "08"
    get last word of b --> returns "1998"

Help from Bash

If your filenames are even more complex, use do shell script and a regular expression match:

    set filename to "G014_1998_AB.jpg"

    return do shell script (["echo ", filename, ¬
        " | egrep -o -e '\\d+' | tail -1"] as text)

    --> returns "1998"

Thanks to @user3439894 for suggesting I implement tail as part of the shell script instead of using AppleScript's get last paragraph of command. It's a little bit slicker.

  • 2
    If you're going to use do shell script why not process it so what's returned is what's wanted and do it in two lines of code not four, e.g. set theString to "G014_1998_AB.jpg" and then set theNumber to do shell script "egrep -o -e '\\d+'<<<'" & theString & "' | tail -1" – user3439894 Jan 18 '18 at 12:55
  • @user3439894 I do prefer your way, in fact. Mind if I update my answer to use your suggestion ? Or is the etiquette such that the alternatives given should remain as comments so the OP can see what was offered versus suggested improvements ? – CJK Jan 18 '18 at 13:09
  • Feel free to use it. :) – user3439894 Jan 18 '18 at 13:46
  • While I find this answer, and it's comments interesting...I don't see these solutions as being able to address the situation of: What if the filenames are all items of a generated list of files, like this... {"G014_1998_24.jpg", "G024_2006_08.jpg", "G024_2006_13.jpg", "G014_1998.jpg", "G014_1998_A.jpg", "G014_1998_AB.jpg"} that the OP would like to pass to the rest of the script to process? – wch1zpink Jan 18 '18 at 19:41
  • @wch1zpink, I don't see where you've quoted that from. If anything, I see a possible connection to a comment against the question that specifically states that looping through a list is not the issue: "They eventually will be in a loop. However, I will take care of the loop structure myself, that's not what I am having issues with. So I need to know how to extract the last digit sequence from a single string (that could take all of the above forms)" – CJK Jan 18 '18 at 19:46
1

This works for me using the latest version of Sierra

property myStrings : {"G014_1998_24.jpg", "G024_2006_08.jpg"}

set strippedStrings to {}

repeat with i from 1 to number of items in myStrings
    set this_item to item i of myStrings
    set end of strippedStrings to items -5 thru -6 of this_item as string
    -- uncomment next line if u prefer integers returned instead (wont retain leading zero's)
    -- set end of strippedStrings to items -5 thru -6 of this_item as string as integer
end repeat

If you want to be able to sort your list in ascending order, this version of the script uses scripting addition SATIMAGE Scripting Addition

property myStrings : {"G014_1998_24.jpg", "G024_2006_08.jpg", "G024_2006_13.jpg"}
property sortedStrippedStrings : {}

set strippedStrings to {}

repeat with i from 1 to number of items in myStrings
    set this_item to item i of myStrings
    set end of strippedStrings to items -5 thru -6 of this_item as string
end repeat
-- Uses SATIMAGE scripting addition to sort list ascending 
set sortedStrippedStrings to sortlist strippedStrings with ascending
  • Thanks @wch1zpink! However, I now realized, I should have been more specific on the possible strings that I could get. My apologies. The position of the last digit sequence is not always the same and I really need to retrieve the last digit sequenze, no matter where it occurs. See my edit above. – mdomino Jan 18 '18 at 9:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .