Extract phrase between two strings with Applescript

Discussion in 'Mac Programming' started by vloryan, Aug 5, 2014.

  1. vloryan macrumors member

    Joined:
    Jan 11, 2014
    #1
    Hi, i'd like to extract a phrase between two strings from a txtfile that was already opened with Automator and save that result to a textfile in my Documents folder!

    **date**August 5th 2014**/date**

    must save a date.txt containing "August 5th 2014"



    Looking forward hearing from you!
     
  2. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #2
    Here's the guts of what you need.

    Code:
    set phrase to "**date**August 5th 2014**/date**"
    set AppleScript's text item delimiters to "**"
    set theList to every text item of phrase
    log theList
    log item 3 of theList
     
  3. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #3
    nice! but how would this look like generally? "August 5th 2014" was just an example and the script should extract anything between "**".

    thank you so much!
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    Or alternatively...

    Code:
    set phrase to "**date**August 5th 2014**/date**"
    set AppleScript's text item delimiters to "**date**"
    set theText to item 2 of every text item of phrase
    set AppleScript's text item delimiters to "**/date**"
    set theText to item 1 of every text item of theText
    set AppleScript's text item delimiters to ""
    ...as with the previous example, it doesn't really matter what you have between your "pseudo" tags. Anything between "**date**" and "**/date**" should be found, no matter what it is.

    Test it and find out! ;-)
     
  5. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #5
    nice. and how can this exctracted phrase be saved to a "date.txt" in the same folder the original txt file is saved?

    thanks!
     
  6. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #6
    Well, you now have a variable called theText which holds the text you want. There are a number of ways you can write text to a file. There's a pretty comprehensive discussion here:

    http://stackoverflow.com/questions/3780985/how-do-i-write-to-a-text-file-using-applescript

    Personally, I like the 'do shell script' method. As always, there are pros and cons and your mileage may vary. So something like:

    Code:
    do shell script "echo " & quoted form of theText & " > ~/Desktop/myfile.txt"
     
  7. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #7
    looks really good until i try to save the phrase to a txt file using

    Code:
    do shell script "echo theText > some_file.txt"
    this results in

    Code:
    Error: "sh:some_file.txt: Permission denied"
    I have tried to find a solution myself, but... sorry ;(
     
  8. kryten2, Aug 6, 2014
    Last edited: Aug 6, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #8
    Try the example from superscape e.g.


    Code:
    do shell script "echo " & quoted form of theText & " > [B]~/Desktop/[/B]myfile.txt"
    From Technical Note TN2065 :

    Info : https://developer.apple.com/library/mac/technotes/tn2065/_index.html
     
  9. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #9
    great! the end is near... :)

    now, how can i change

    Code:
    do shell script "echo " & quoted form of theText & " > ~/Desktop/myfile.txt"
    so that the file is saved in the same folder as the orginal text file? i have an AUTOMATOR action at the very top that finds that txt-file.

    Thaaaaanks!
     
  10. kryten2, Aug 7, 2014
    Last edited: Aug 7, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
    No not quite. Do we have to guess where the original text file is located? Where was the myfile.txt created when you ran the example code? In your Desktop folder. ~/Desktop/myfile.txt is just an abreviation for /Users/yourusername/Desktop/myfile.txt. If you look at the result of that Automator action at the very top that finds the txt file you'll have the answer to your question. I also strongly suggest that if you want to continue automating stuff that you start reading some tutorials regarding Automator, Applescript, navigating the file system from the command line using Terminal, Posix paths etc.

    Tip : click Results in the Automator action and from the view options choose the middle one to see the path of the original txt file.
     
  11. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #11
    i've already started reading and hopefully learning books about applescript :).

    any chance to extract the path of that txt file in a variable and use this variable in the end to save that file?
     
  12. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #12
    I'm sure that's doable, yes. ;-)
     
  13. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
  14. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #14
    sorry, me again :(

    Code:
    set phrase to "**date**August 5th 2014**/date**"
    set AppleScript's text item delimiters to "**date**"
    set theText to item 2 of every text item of phrase
    set AppleScript's text item delimiters to "**/date**"
    set theText to item 1 of every text item of theText
    set AppleScript's text item delimiters to ""
    do shell script "echo " & quoted form of theText & " > ~/Desktop/myfile.txt"
    the text between **date** and **/date**" isn't extracted, only "August 5th 2014". But this line doesn't even exist in my txt file...

    looks like nothing is found ;(
     
  15. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #15
    You're not doing anything with your txt file and you found what you were looking for in the snippet you posted eg August 5th 2014.
     
  16. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #16
    I don't get it. I have a txt with different phrases between **date** and **/date** and i want to save those to a new txt. Using this code above doesn't work for me ;(
     
  17. kryten2, Aug 8, 2014
    Last edited: Aug 8, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #17
    The example given to you will extract the August 5th 2014 part of the string "**date**August 5th 2014**/date**" set in the variable phrase by means of text item delimiters.

    Code:
    set [COLOR="Red"]phrase[/COLOR] to [B]"**date**August 5th 2014**/date**"[/B]
    set AppleScript's text item delimiters to "**date**"
    set theText to item 2 of every text item of [COLOR="red"]phrase[/COLOR]
    set AppleScript's text item delimiters to "**/date**"
    set theText to item 1 of every text item of theText
    set AppleScript's text item delimiters to ""
    do shell script "echo " & quoted form of theText & " > ~/Desktop/myfile.txt"
    If you want to extract something from text/strings in your txt file you'll first need to get that text or those strings from your txt file to work with. The example code knows nothing of your txt file e.g. where it is, what the content is etc. because you haven't provided that information to it.

    Info : WorkingWithText and read-textfile-into-list-in-applescript
     
  18. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #18
    I am searching for that txt-file in automator, open it with automator and then i run this applescript in automator. wrong?
     
  19. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #19
    sorry for asking, but: any news on this? :) thanks!
     
  20. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #20
    "**date**August 5th 2014**/date**" was hardcoded as an example on how to extract a phrase between **date** and **/date**

    You now have to swap the hardcoded string to a variable containing your desired string
     
  21. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #21
    thanks, but i have no idea how to do this with a variable, so i surrender! thanks everybody for you help!
     
  22. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #22
    There's no need to surrender. Don't give up so easily. Here's an example with 5 dates to extract from a text file.

    In Automator :

    1. Find Finder Items
    2. Run Applescript with the following code :

      Code:
      on run {input, parameters}
      	(* Your script goes here *)
      	tell application "Finder" to set fileContainer to container of first item of input
      	set myText to paragraphs of (read first item of input as «class utf8»)
      	set finalText to ""
      	repeat with nextLine in myText
      		if nextLine is not "" then
      			set AppleScript's text item delimiters to "**date**"
      			set theText to item 2 of every text item of nextLine
      			set AppleScript's text item delimiters to "**/date**"
      			set theText to item 1 of every text item of theText
      			set AppleScript's text item delimiters to ""
      			set finalText to finalText & theText & return
      		end if
      	end repeat
      	do shell script "echo " & quoted form of finalText & " > " & quoted form of POSIX path of (fileContainer as alias) & "date.txt"
      	return finalText
      end run
     

    Attached Files:

Share This Page