PDA

View Full Version : Need help with Applescript



kurisu
4th October 2008, 11:30 PM
HI there,

I want to extract keywords from a CSV file to a text file, and in turn to put all the keywords in the text file to spotlight comment. One line from applescript is:
do shell script "echo " & quoted form of formatted_keyword & ">>" & text_file
where formatted_keyword = the individuals keywords and text_file is the filename of the output file.

However, it seems like the applescript I'm fiddling with insert the keyword with "return key" in between, so the text file is something like:

*blank with return keystoke*
&KeywordA
&KeywordB
&KeywordC
&KeywordD

Is it possible to convert this textfile with applescript to become something like this:

&KeywordA &KeywordB &KeywordC &KeywordD

I just started learning applescript few days ago, so I'm sorry if this is a stupid straight-forward question.

P.S. do shell script "echo " & quoted form of formatted_keyword & ">>" & text_file
Cheers,
Chris

kurisu
6th October 2008, 07:28 AM
no one?

stewiesno1
6th October 2008, 08:18 AM
I'm no Applescript user kurisu but I remember seeing a part of Apples forums ( Apple-Support-Discussions ) had a dedicated Applescript page...
Apple - Support - Discussions - AppleScript (http://discussions.apple.com/forum.jspa?forumID=724)
Maybe post this question there and I am sure one of the gurus would be able to post some info for you.

Stewie

Venom71
6th October 2008, 08:30 AM
Applescript Forums | MacScripter / AppleScript | OS X (http://bbs.macscripter.net/viewforum.php?id=2)

Nevets_Anderson
8th October 2008, 06:02 AM
It's a bit early so my brain may not be much use - From what I can see and understand, The issue is with the embeded shell script your using not the actual applescript

If you open terminal and just do an

echo "my bit of text" >> mytextfile.txt
echo "my bit of text2" >> mytextfile.txt

Then it's the shell script thats putting the return into the code

You may like to try arranging your script so that you do something like

echo "bit of text1 " "Bit of text2 " "bitof text3 " >> testfile.txt

This would put all 3 bits of text on the one line

Hope this helps

Nevets

jonargall
8th October 2008, 07:11 AM
Personally I'm fairly limited with AppleScript, I almost always use bash scripts (they run in the Terminal without need for AppleScript). Here's a script that will convert all line-endings to spaces in a text file:

#!/bin/bash
TEXTFILE='insert the name of your text file here'
perl -pi -e 's/\r\n/\n/g' "$TEXTFILE"
perl -pi -e 's/\r/\n/g' "$TEXTFILE"
perl -pi -e 's/\n/ /g' "$TEXTFILE"

If you want to run this from within AppleScript, this is how you would type it:

do shell script "TEXTFILE='insert the name of your text file here' ; perl -pi -e 's/\r\n/\n/g' \"$TEXTFILE\" ; perl -pi -e 's/\r/\n/g' \"$TEXTFILE\" ; perl -pi -e 's/\n/ /g' \"$TEXTFILE\""

Does that work for you?

jonargall
8th October 2008, 08:57 AM
do shell script "echo " & quoted form of formatted_keyword & ">>" & text_file
Or try this instead

do shell script "echo -n" & quoted form of formatted_keyword & "\ >>" & text_file

The -n after echo will leave out the return character.
The \ (backslash before the space) will insert a space at the end of the keyword.

soulman
8th October 2008, 09:44 AM
Agree with Nevets & Jon that you are trying to fix a problem you can avoid having but, FWIW, this kind of thing is pretty straightforward to do in AS. Personally, I would read the original file using AS and pull the keywords straight out but I don't know what you've got in the file so I can't offer any suggestions on that.

Anyway, this, from The FooDoo Lounge (http://www.foodoo.sunreal.com.au/), will do the find/replace you are asking for. Copy it to Script Editor and run it:


set inputText to "
&KeywordA
&KeywordB
&KeywordC
&KeywordD"

set outputText to findReplace for inputText from {"
"} into {" "}

-- findReplace -- by Richard Morton, 2002 --
-- Multiple find/replace
-- Pass a string, a list of 'find' strings and a list of 'replace' strings
to findReplace for inputString from oldChars into newChars
tell AppleScript
set olTids to text item delimiters
copy inputString to tempStr
try
repeat with n from 1 to length of oldChars
set text item delimiters to item n of oldChars
set tmpList to text items of tempStr
set text item delimiters to item n of newChars
set tempStr to text items of tmpList as string
end repeat
set text item delimiters to olTids
on error eStr number eNum
set text item delimiters to olTids
error eStr number eNum
end try
return tempStr
end tell
end findReplace
One thing you need to be aware of is that there are three different kinds of return in use - Macs traditionally used a CR (carriage return) but this is less common in OS X, UNIX & many parts of X use LF (the linefeed char) and Windows uses both together.