Reply
   
 Need help with Applescript 
 
 
  #1 (permalink)  
Old 04-10-2008, 10:30 PM
Member

Group: Regulars
Location: Melbourne


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
kurisu is offline
Profile CardPM
Go to the top of the page
Reply With Quote
  #2 (permalink)  
Old 06-10-2008, 07:28 AM
Member

Group: Regulars
Location: Melbourne


no one?
kurisu is offline
Profile CardPM
Go to the top of the page
Reply With Quote
  #3 (permalink)  
Old 06-10-2008, 08:18 AM
Regular

Group: Regulars
Location: Northern Beaches , Sydney


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
stewiesno1 is offline
Profile CardPM
Go to the top of the page
Reply With Quote
  #4 (permalink)  
Old 06-10-2008, 08:30 AM
Regular

Group: Regulars
Location: Gold Coast


Applescript Forums | MacScripter / AppleScript | OS X
__________________
On the whole, I'd rather be in Philadelphia ...
Venom71 is offline
Profile CardPM
Go to the top of the page
Reply With Quote
  #5 (permalink)  
Old 08-10-2008, 06:02 AM
Regular

Group: Regulars
Location: Melb
Blog Entries: 1


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
Nevets_Anderson is offline
Profile CardPM
Go to the top of the page
Reply With Quote
  #6 (permalink)  
Old 08-10-2008, 07:11 AM
Regular

Group: Regulars


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.
jonargall is offline
Profile CardPM
Go to the top of the page
Reply With Quote
  #7 (permalink)  
Old 08-10-2008, 08:57 AM
Regular

Group: Regulars


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.
jonargall is offline
Profile CardPM
Go to the top of the page
Reply With Quote
  #8 (permalink)  
Old 08-10-2008, 09:44 AM
Regular

Group: Regulars
Location: NE Vic


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.
soulman is offline
Profile CardPM
Go to the top of the page
Reply With Quote
 
Reply

Thread Tools

 
Similar Threads
 
Thread Thread Starter Forum Replies Last Post
Automator / Applescript help dann Help and New Mac User Support 1 06-04-2008 07:29 AM
applescript record doesnt record mail.app Asher Tuzza Projects: Audio, Graphics, Video, HTPC and Programming 4 12-10-2007 05:57 PM
AppleScript App - Please Help! pharmy Projects: Audio, Graphics, Video, HTPC and Programming 2 09-09-2007 07:24 PM
Recommend an Applescript book forgie Projects: Audio, Graphics, Video, HTPC and Programming 5 25-07-2007 03:04 PM
Using POSIX path in Applescript Kuzey Mac OS X & All Software 5 16-03-2007 10:09 PM