Help to process text using Applescript

Discussion in 'Mac Programming' started by MMichal, Jan 17, 2010.

  1. MMichal macrumors newbie

    Joined:
    Jan 17, 2010
    #1
    Hi, I am really new to Applescript so I am looking for someone to help me

    Text:

    07 Street
    Place: 528|452
    Points: 10.000
    Name: bomberman

    I want to process it using Applescript to get (and finally using it in Textexpander):

    bomberman / 07 Street / 528|452

    I hope you all understand what I want :)
     
  2. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    Code:
    set blob to "07 Street
    Place: 528|452
    Points: 10.000
    Name: bomberman"
    
    set output to word 2 of paragraph 4 of blob & " / " & ¬
    	paragraph 1 of blob & " / " & words 2 thru -1 of paragraph 2 of blob
    
    This is close ... what's the big batch of data look like? Can you just break off four-paragraph blobs and process them?

    mt
     
  3. MMichal thread starter macrumors newbie

    Joined:
    Jan 17, 2010
    #3
    Thanks that's exactly it :) working fine

    this is how it looks for Textexpander

    Code:
    set blob to (the clipboard as string)
    
    set output to word 2 of paragraph 4 of blob & " / " & ¬
    	paragraph 1 of blob & " / " & words 2 thru -1 of paragraph 2 of blob
    But one little question ... if the name was 2 words e.g. Super Mario how would you write the first part to select these 2 words from that line? Thanks
     
  4. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #4
    Code:
    set blob to "07 Street
    Place: 528|452
    Points: 10.000
    Name: Super Mario Bros"
    
    set output to ((characters 7 thru -1 of paragraph 4 of blob) as string) & " / " & paragraph 1 of blob & " / " & words 2 thru -1 of paragraph 2 of blob
    
    The easy thing would be to say ... "set output to words 2 thru - 1 of paragraph 4 of blob ..."

    The trouble is that AppleScript then returns a list and each word is an item in the list. If you coerce the first bit as a string, it returns "SuperMarioBros". So in this one we ask for characters, then coerce it as a string. As characters, the spaces aren't ignored, and it returns "Super Mario Bros"

    What's curious is that "words 2 thru -1 of paragraph 4" turns the output into a list, but "words 2 thru -1 of paragraph 2" at the end doesn't affect the output. That may well be a "bug" in AppleScript, so watch this script as new versions of AS are released.

    mt
     
  5. MMichal thread starter macrumors newbie

    Joined:
    Jan 17, 2010
    #5
    Yes as you said the easy thing isn't working .. I tried that with the same result as yours.
    But it works fine with characters and string.
    Thanks
     
  6. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #6
    [First, too weird to be quoting myself]

    I hit the "Submit Reply" button, shut down the computer, climbed into bed and then realized that this probably IS NOT a bug, but in fact quite logical.

    The reason, I believe, is that the first part of the variable is evaluated as a string, therefore the concatenations to the variable just make the string longer. The alternative evaluates as a list, therefore the concatenations just add items to the list. At least that's why I think there's a difference and why there's really no reason to worry about future AS versions. Time will tell.

    mt
     

Share This Page