Applescript application read from file

喜你入骨 提交于 2019-12-23 12:26:16

问题


I have a compiled AppleScript application which I have moved to my windows server. I'd like to then insert a text file into the application (which looks like a zip file on windows):

myapplescript.app/Contents/Resources/MyNewDir/MyTxtFile.txt

So, I've precompiled the AppleScript to try to read from this text file and get the contents as a string. This is what I do:

set theFolder to POSIX path of (the path to me)
set theFile to theFolder & "Contents/Resources/MyNewDir/MyTxtFile.txt"
open for access theFile
set fileContents to (read theFile)
close access theFile

but this is the error I get:

Can't make "/Users/mike/Desktop/myapplescript.app/Contents/Resources/MyNewDir/MyTxtFile.txt" into type file


回答1:


Ok, I figured it out, I changed the second line to this:

set theFile to (POSIX file (theFolder & "Contents/Resources/MyNewDir/MyTxtFile.txt"))



回答2:


There is also a single line version of read:

read POSIX file "/tmp/test.txt" as «class utf8»

Both versions use MacRoman unless you add as «class utf8». (as Unicode text is UTF-16.)




回答3:


Reading a file via a file path in a variable.

The 1st two work. The 3rd, which stores the file name in variable does not.

set myData to read file POSIX file ¬ "/Users/sww/Devel/afile.csv"

set myData to read file ¬ "Macintosh HD:Users:sww:Devel:afile.csv"

set fRef to "Macintosh HD:Users:sww:Devel:afile.csv"

set myData to read file fRef  -- No good

To fix? Give the file reference as a string.

set myData to read file (fRef as string)  -- OK


来源:https://stackoverflow.com/questions/281108/applescript-application-read-from-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!