Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 03-21-2008, 02:21 PM   #1
dougyitbos
Registered User
 
Join Date: Mar 2008
Posts: 2
read a file in an applescript.

Hi folks..
I'm trying to read a file in an applescript.


I've tried a number of different syntax options.
The most simple is:

read theTextFile

I get the following error:
Expected end of line, etc. but found identifier.


But this is the most basic syntax I've found.
Any ideas?
dougyitbos is offline   Reply With Quote
Old 03-21-2008, 03:06 PM   #2
cwtnospam
League Commissioner
 
Join Date: Jan 2005
Posts: 8,475
Try it this way:

Code:
tell application "TextEdit"
	set x to posix file "/path/to/theTextFile.txt" as alias
	open x
	set file_text to text of document 1
end tell
cwtnospam is offline   Reply With Quote
Old 03-21-2008, 03:41 PM   #3
mark hunte
MVP
 
Join Date: Apr 2004
Location: Hello London Calling
Posts: 1,787
I Thing the OP wants to read the file, rather than open it.

Code:
set x to posix file "/path/to/theTextFile.txt" as alias
read x
or

Code:
read alias "Macintosh HD:path:to:theTextFile.txt"
__________________
MH.
mark hunte is offline   Reply With Quote
Old 03-21-2008, 04:07 PM   #4
dougyitbos
Registered User
 
Join Date: Mar 2008
Posts: 2
no good

No good.

Same thing.
Won’t compile.
Says:
Expected end of line, etc. but found class name.

And highlights whatever follows "read"

ugh... weird.
dougyitbos is offline   Reply With Quote
Old 03-21-2008, 06:09 PM   #5
cwtnospam
League Commissioner
 
Join Date: Jan 2005
Posts: 8,475
Is your "read x" line inside a tell?
cwtnospam is offline   Reply With Quote
Old 03-21-2008, 11:00 PM   #6
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
Code:
set fp to open for access <the filepath>
set theFileContents to read fp
close access fp
__________________
Philosophy is a battle against the bewitchment of our intelligence by means of language. -LW-
tw is offline   Reply With Quote
Old 03-22-2008, 04:40 AM   #7
mark hunte
MVP
 
Join Date: Apr 2004
Location: Hello London Calling
Posts: 1,787
I Think cwtnospam is right the error is most likely in the rest of the script.
Can you let use see all the script as you are doing it.

Tw, the open for access is for writing to the file AFAIK, and is not needed to just read a file.
But not really knowing what the OP wants to really do who knows if they will need it?
__________________
MH.
mark hunte is offline   Reply With Quote
Old 03-22-2008, 07:53 PM   #8
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
Quote:
Originally Posted by mark hunte
Tw, the open for access is for writing to the file AFAIK, and is not needed to just read a file.
But not really knowing what the OP wants to really do who knows if they will need it?

actually, open for access is for either reading or writing (read the standard additions scripting dictionary). my only other thought, though, is that maybe he's not quoting the or parenthesizing the file name correctly?
__________________
Philosophy is a battle against the bewitchment of our intelligence by means of language. -LW-
tw is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

Forum Jump



All times are GMT -5. The time now is 06:47 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.