MacTalk Forums > Mac Stuff > Mac OS X
Mac OS X Discussion and support on Mac OS X. If you have an issue with an application (e.g: Safari, or iPhoto, or Adium), it goes in the Applications forum.


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 4th October 2008, 11:30 PM
 
Join Date: May 2005
Location: Melbourne
Posts: 114
Default Need help with Applescript

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
Reply With Quote
  #2 (permalink)  
Old 6th October 2008, 08:28 AM
 
Join Date: May 2005
Location: Melbourne
Posts: 114
Default

no one?
Reply With Quote
  #3 (permalink)  
Old 6th October 2008, 09:18 AM
 
Join Date: Jun 2006
Location: Northern Beaches , Sydney
Posts: 1,823
Default

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
Maybe post this question there and I am sure one of the gurus would be able to post some info for you.

Stewie
__________________
Lotsa Macs - PM's , G3's , G4's & Powerbooks - Love 'em
----------------
Painless trades : William , Clockwork , Brains, Applecollector, Simo, TimWallG5, ric3am, JMD , Forgie, Avolve, Zen, Mctastic, iCant
Freebies from : Clockwork, TimRyan twice, Asphotos twice , DebB , Avolve , Froggy, Bee-J , Stepandy, Hoony
Reply With Quote
  #4 (permalink)  
Old 6th October 2008, 09:30 AM
 
Join Date: Jan 2006
Location: Gold Coast
Posts: 366
Default

Applescript Forums | MacScripter / AppleScript | OS X
__________________
On the whole, I'd rather be in Philadelphia ...
Reply With Quote
  #5 (permalink)  
Old 8th October 2008, 07:02 AM
Nevets_Anderson's Avatar  
Join Date: Jun 2004
Location: Melb
Posts: 319
Default

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
__________________
Successful trades witn ilostmypassword
Reply With Quote
  #6 (permalink)  
Old 8th October 2008, 08:11 AM
jonargall's Avatar  
Join Date: Oct 2005
Posts: 225
Default

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?
__________________
Successful trades:
sold Final Cut Express to Silver.
bought a random PC serial cable from Simo.
Reply With Quote
  #7 (permalink)  
Old 8th October 2008, 09:57 AM
jonargall's Avatar  
Join Date: Oct 2005
Posts: 225
Default

Quote:
Originally Posted by kurisu View Post
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.
__________________
Successful trades:
sold Final Cut Express to Silver.
bought a random PC serial cable from Simo.
Reply With Quote
  #8 (permalink)  
Old 8th October 2008, 10:44 AM
soulman's Avatar
Electric Sheep
 
Join Date: Apr 2004
Location: NE Vic
Posts: 1,266
View soulman's Flickr Page
Default

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, will do the find/replace you are asking for. Copy it to Script Editor and run it:

Code:
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.
Reply With Quote
Reply

Bookmarks

Tags
applescript


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On