Trying to append data to file. Write permission error

I am trying to take an Applescript and use it in a much larger ApplescriptObjC project and I have ironed out most differences. Except for one:

The old Applescript gathered some data into a string and append it to a .csv file and It worked! The code was lifted from here: http://macscripter.net/viewtopic.php?id=36861.

In the ApplescriptObjC project in xcode I get this error:

“2015-01-06 04:54:21.729 Tenniskamp[30481:1067784] *** -[AppDelegate doProcess:]: Network file permission error. (error -5000)”

Even if I try to use a non existing file the same error occur.

The essential lines of code looked like this in Applescript:


set filePath to ("Macintosh HD:Users:anders:Desktop:" as text) & "kamp.csv" ”in the top of the file
.
set theResult to writeTo(filePath, theString, text, true) ”just after I have set theString to the info I want to save
.
on writeTo(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		set openFile to open for access file targetFile with write permission
		if apendData is false then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeTo

With modifications (POSIX file path and a tell current application block) it looks like this in Xcode:


set filePath to "/Users/anders/Desktop/kamp.csv"
.
set theResult to writeTo(filePath, theString, text, true)
.

    on writeTo(targetFile, theData, dataType, apendData)
        -- targetFile is the path to the file you want to write
        -- theData is the data you want in the file.
        -- dataType is the data type of theData and it can be text, list, record etc.
        -- apendData is true to append theData to the end of the current contents of the file or false to overwrite it

            set targetFile to targetFile as text
            tell current application
                set openFile to open for access file targetFile with write permission
            end tell
            if apendData is false then set eof of openFile to 0
            write theData to openFile starting at eof as dataType
            close access openFile
            return true


    end writeTo

without the tell current application block I get this warning:

“2015-01-06 05:11:07.658 Tenniskamp[31149:1078147] *** -[AppDelegate doProcess:]: Can’t make current application into type «class fsrf». (error -1700)”

Any suggestions why I get the first error message?

On 10.10 and Xcode 6.1

Try wrapping your whole try block in a “tell current application” block. You can’t sue the read/write commands and file references the same in an ASObjC project in Xcode as you do elsewhere.

Thanks Shane. The POSIX file path and tell current application change from AS to ASOC was from your book. I hope I understood it correctly.

Like this?

    on writeTo(targetFile, theData, dataType, apendData)
        -- targetFile is the path to the file you want to write
        -- theData is the data you want in the file.
        -- dataType is the data type of theData and it can be text, list, record etc.
        -- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
            tell current application
            set targetFile to targetFile as text

                set openFile to open for access file targetFile with write permission

            if apendData is false then set eof of openFile to 0
            write theData to openFile starting at eof as dataType
            close access openFile
            return true
            end tell
    end writeTo
    

I get the same result :expressionless:

I have removed all “try” parts because I want to know what going on.

I don’t think the permissions error relates to your code. To test it, try another file somewhere else.

I found a solution :slight_smile:

instead of

tell current application
.
end tell 

this did the trick

tell application "System Events"
tell process "Finder"
.
end tell
end tell

The outer tell block is doing nothing there. And it sounds like you might have already opened the file in a Finder tell block.

It might be working for you, but you’d really be better tracking down the cause if possible.