Here is a script that I have used dozens of times in Applescript and I am trying to get it to work in Applescript Studio. I keep getting an error on the “set text item delimiters” command and can’t figure out why. Any help would be greatly appreciated.
global strList
on clicked theObject
tell window of theObject
if the name of theObject is "btnDataFile" then
set strTextFile to choose file with prompt "Choose a tab-delimited text file" of type {"TEXT"}
set strTextFileDisplay to strTextFile as string
set contents of the text field "txtDataFile" to strTextFileDisplay
open for access strTextFile
set strDataFile to read strTextFile using delimiter return
close access strTextFile
set strRecordCount to count of strDataFile
set contents of the text field "txtRecordCount" to strRecordCount - 1
set strList to {}
set text item delimiters to tab
repeat with intCounter from 1 to count of strDataFile
set strLine to text items of item intCounter of strDataFile
copy strLine to the end of strList
end repeat
set text item delimiters to ""
display dialog "Completed"
end if
end tell
end clicked
tell window of theObject
set AppleScript's text item delimiters to tab
I think if you are inside a tell block to the window, you have to specify AppleScript. I just tested it in and out of a “tell window 1” block and it bombs if you don’t include the “AppleScript’s”.
Delimiters are a property of applescript itself, so I think the errors you are having might be a result of referencing the delimiter property incorrectly. In place of your current delimiter code, you probably want something like this instead…
set oldDelimiter to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
repeat with intCounter from 1 to count of strDataFile
set strLine to text items of item intCounter of strDataFile
copy strLine to the end of strList
end repeat
set AppleScript's text item delimiters to oldDelimiter
Also, ‘tell ’ blocks surrounding entire blocks of code can cause problems, because the object does not recognize the command your sending it. In this case, it appears that you are sending the WINDOW the delimiter commands, rather than sending it to applescript.
I would think something like the following could successfully replace your code. If this doesn’t help, let us know what the specific error’s you’re getting are.
global strList
on clicked theObject
set theWindow to (name of window of theObject)
if the name of theObject is "btnDataFile" then
set strTextFile to choose file with prompt "Choose a tab-delimited text file" of type {"TEXT"}
set strTextFileDisplay to strTextFile as string
set contents of the text field "txtDataFile" of window theWindow to strTextFileDisplay
open for access strTextFile
set strDataFile to read strTextFile using delimiter return
close access strTextFile
set strRecordCount to count of strDataFile
set contents of the text field "txtRecordCount" of window theWindow to (strRecordCount - 1)
set strList to {}
set oldDelimiter to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
repeat with intCounter from 1 to count of strDataFile
set strLine to text items of item intCounter of strDataFile
copy strLine to the end of strList
end repeat
set AppleScript's text item delimiters to oldDelimiter
display dialog "Completed"
end if
end clicked
There are several optimizations in this code. You don’t have to open a file to read it; using paragraphs instead of the delimiter return allows you to read files with any kind of line endings; you don’t waste memory by assigning variables you don’t need; you’re not copying data in the repeat loop which is faster; etc.