Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applescript: read text from file

I can't read text from text file using applescript. Tried this:

set sharesFileName to (the POSIX path of (path to home folder)) & "Applications/mounts"
set sharesLines to paragraphs of (read POSIX file sharesFileName as "class utf8" using delimiter linefeed)

Got "Script Error", "End of file error"

What am I doing wrong? File, surely, exists and readable

like image 233
BbIKTOP Avatar asked Jan 29 '26 21:01

BbIKTOP


1 Answers

If mounts is a plain text file, then the following example AppleScript code should work:

set sharesFileName to (the POSIX path of (path to home folder)) & "Applications/mounts"
set sharesLines to read sharesFileName

Example:

$ cat mounts
one
two
three
$

Using:

set sharesLines to read sharesFileName

Results:

"one
two
three
"

Using:

set sharesLines to read sharesFileName using delimiter linefeed

Results:

{"one", "two", "three"}

Using:

set sharesLines to paragraphs of (read sharesFileName)

Results:

{"one", "two", "three", ""}

As you can see the last example includes the last linefeed as an empty string in the file, where using using delimiter linefeed does not, and both create a list. So if you want sharesLines to be as list, then use:

set sharesLines to read sharesFileName using delimiter linefeed

If you want sharesLines to just contain the contents of mounts as paragraphs of text, then use:

set sharesLines to read sharesFileName

Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

like image 178
user3439894 Avatar answered Feb 03 '26 00:02

user3439894