Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

vloryan

macrumors member
Original poster
Jan 11, 2014
57
0
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!
 
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
 
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!
 
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! ;-)
 
nice. and how can this exctracted phrase be saved to a "date.txt" in the same folder the original txt file is saved?

thanks!
 
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"
 
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 ;(
 
I have tried to find a solution myself, but... sorry ;(

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 :

What’s the default working directory for do shell script commands?

do shell script inherits the working directory of its parent process. For most applications, such as Script Editor, this is /. For osascript, it’s the working directory of the shell when you launched osascript. You should not rely on the default working directory being anything in particular. If you need the working directory to be someplace specific, set it to that yourself.
Info : https://developer.apple.com/library/mac/technotes/tn2065/_index.html
 
Last edited:
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!
 
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!

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.
 
Last edited:
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?
 
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...

Anything between "**date**" and "**/date**" should be found, no matter what it is

looks like nothing is found ;(
 
sorry, me again :(


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 ;(

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.
 
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 ;(
 
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 ;(

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
 
Last edited:
I am searching for that txt-file in automator, open it with automator and then i run this applescript in automator. wrong?
 
"**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
 
thanks, but i have no idea how to do this with a variable, so i surrender! thanks everybody for you help!
 
thanks, but i have no idea how to do this with a variable, so i surrender! thanks everybody for you help!

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
 

Attachments

  • Screen Shot 2014-08-12 at 03.14.22.png
    Screen Shot 2014-08-12 at 03.14.22.png
    27.7 KB · Views: 268
  • Screen Shot 2014-08-12 at 03.31.12.png
    Screen Shot 2014-08-12 at 03.31.12.png
    80.2 KB · Views: 312
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.