Folks,
I’ve been trying to get text saved to a file without any success and thought someone might be able to identify where I’m going wrong. I have looked at other threads and don’t see anything that I’m doing wrong. The dialog displays but when select a file that it can write to (or specify a new file) nothing is getting written to the file. In the case of a new file it’s not even being created. Here’s what I’m doing.
I’ve got a button on a window to click to save the file. When that button is clicked, it runs a procedure that opens a save dialog:
on saveMapping()
tell save panel
set title to "Save mapping as..."
set can choose directories to false
set can choose files to true
set prompt to "Save"
set required file type to "txt"
set treat packages as directories to false
end tell
display save panel attached to window "mappedWindow"
end saveMapping
And I have checked off that the mappedWindow in IB should process an event for panel ended.
Then in the panel ended process I have the result of the save dialog used as the location that the data is written to:
on panel ended theObject with result withResult
global irisLocation, saveMappingLocation
-- The Preferences window IRIS location choose button was clicked and a directory location was chosen.
if theObject is the open panel and withResult is 1 then
set irisLocation to (item 1 of (path names of open panel as list)) & "/" as text
set contents of text field "irisLocationField" of tab view item "irisTabFields" of tab view "prefWindowTabs" of window "prefWindow" to irisLocation as text
else if theObject is the save panel then
if withResult is 1 then
set saveMappingLocation to (path name of save panel)
my writeToFile(saveMappingLocation, "This is a test. This is just a test.", false)
--log saveMappingLocation
end if
end if
end panel ended
When I have the log feature enabled, the value of the saveMappingLocation variable does contain a Unix style path and filename specification of the desired file.
The writeToFile function looks like this:
on writeToFile(theFile, theString, appending)
--set theFile to theFile as string
set theFile to theFile as file specification
try
open for access file theFile with write permission
if (appending = false) then
set eof of theFile to 0
end if
write theString to theFile starting at eof
close access theFile
on error
try
close access theFile
end try
end try
end writeToFile
As you can see I’ve tried it with theFile as both type string and file specification. Neither appears to work.
Anybody have any suggestions or ideas?
On a related issue, when my save dialog appears, I can not have it choose a file and have it be the file name that displays in the text box that a file name gets typed into. Is this possible? If so, is there an option I need to set for that to happen?
Thanks in advance,
Jack