I’m trying to take the contents of a text file and pass these as a variable to an existing AppleScript I have. Unfortunately, when I run this, I get the following error:
“Can’t make “long path to file” into file type.”
Here is the script I’m working with:
set theFile to (path to home folder as text) & "Library:Preferences:OmegaT:script:source.txt"
open for access theFile
set txtToTrans to (read theFile)
close access theFile
set txtToTrans to the clipboard
set txtToTrans to rawURLEncode(txtToTrans)
on rawURLEncode(str)
return do shell script "/bin/echo -n " & quoted form of str & " | php -r ' echo rawurlencode(fgets(STDIN)); '"
end rawURLEncode
set rawURLEncodeToTrans to do shell script "curl " & quoted form of ("http://mymemory.translated.net/api/get?q=" & txtToTrans & "&langpair=de|en")
set theFilteredText to getTextBetweenDelimiters(rawURLEncodeToTrans, "{\"responseData\":{\"translatedText\":\"", "\"},\"responseDetails\"")
set the clipboard to theFilteredText
on getTextBetweenDelimiters(theText, firstDelim, secondDelim)
try
set {tids, text item delimiters} to {text item delimiters, firstDelim}
set a to second text item of theText
set text item delimiters to secondDelim
set b to first text item of a
set text item delimiters to tids
return b
on error
return theText
end try
end getTextBetweenDelimiters
display alert "Process complete"
I can only assume that in setting the file path I’ve made some sort of syntax error, but I can’t figure out what.
The error message says that the read command expects a file specifier.
The variable theFile contains only a literal string path.
So add the keyword file. Open and close is not necessary
set theFile to (path to preferences folder as text) & "OmegaT:script:source.txt"
set txtToTrans to (read file theFile)
set theFile to (path to preferences folder as text) & "OmegaT:script:source.txt"
set txtToTrans to (read file theFile)
set txtToTrans to the clipboard
set txtToTrans to rawURLEncode(txtToTrans)
on rawURLEncode(str)
return do shell script "/bin/echo -n " & quoted form of str & " | php -r ' echo rawurlencode(fgets(STDIN)); '"
end rawURLEncode
set rawURLEncodeToTrans to do shell script "curl " & quoted form of ("http://mymemory.translated.net/api/get?q=" & txtToTrans & "&langpair=de|en")
set theFilteredText to getTextBetweenDelimiters(rawURLEncodeToTrans, "{\"responseData\":{\"translatedText\":\"", "\"},\"responseDetails\"")
set the clipboard to theFilteredText
on getTextBetweenDelimiters(theText, firstDelim, secondDelim)
try
set {tids, text item delimiters} to {text item delimiters, firstDelim}
set a to second text item of theText
set text item delimiters to secondDelim
set b to first text item of a
set text item delimiters to tids
return b
on error
return theText
end try
end getTextBetweenDelimiters
display alert "Process complete"
When I run this, however, I get the following error:
error “End of file error.”
I’m not sure what the problem is as I can see what you mean about referencing the file (and its contents).
You’re absolutely right. I completely skipped over that. Sorry!
However, even after removing that line, I still get the same error.
error “End of file error.”
BTW - the section “(read file theFile)” from the line “set txtToTrans to (read file theFile)” highlighted in the script editor when the error is thrown.
Probably the file wasn’t closed for access after your previous error and subsequent runs keep using the same access, whose file mark will be at the end of the file after the first successful read. Try saving the script and quitting AppleScript Editor. That’ll close all AppleScript Editor’s accesses to any files. You can then open it again and try running the script.
I’m going to need to ticker some, though, as it behaves rather oddly compared to the old version of the script, which relied on me copying the text by hand and then running the rest of the script.
This new version inserts all sorts of random characters that don’t appear otherwise.
What sort of text does the file contain? Mac Roman? UTF-8? Big-endian or little-endian UTF-16? The script assumes Mac Roman. If it’s any of the other three, the ‘read’ command will need an extra parameter.
For historical reasons, it’s Mac Roman that’s “simple” in the File Read/Write commands. If the file does contain UTF-8 text, the ‘read’ line in the script should be:
set txtToTrans to (read file theFile as «class utf8»)
The ‘as’ here is a labelled parameter of ‘read’, not the AppleScript coercion; and «class utf8» is a token for which Apple has never got round to implementing a keyword!
If the text that is read from a file, is either “rawUrlEncoded” before or after it is read, it is going to look abit “funny”, with %'s and such. It is just a guess, as I don’t know why the text is rawUrlEncoded. But to me, a reason for such an operation, would be to decode it, after it was gotten by curl.
This is just speculations of course. Maybe it would be good to try and get the text afresh, and try reading it without any encoding operation, if the text then looks “funny”, it would be a revealing experiment, to try to decode it, and see if that helps.