Search code examples
applescriptread-write

Cannot open text file to write to with Apple script


I am trying to create a log of when my computer turns off and on. To do this I am writing a script to run at start up that will write to a text file however it is telling me that I don't have permission to write to the file.

Using print statements I have determined that the try block terminates after the first line.

writeTextToFile(getTimeInHoursAndMinutes(), "Users/labspecialist/Desktop/system_log")

on writeTextToFile(theText, theFile)
    try

        -- Convert the file to a string
        set theFile to theFile as string

        -- Open the file for writing
        set theOpenedFile to (open for access file theFile with write permission)   

        -- Write the new content to the file
        write theText to theOpenedFile starting at eof

        -- Close the file
        close access theOpenedFile

        -- Return a boolean indicating that writing was successful
        return true

        -- Handle a write error
    on error

        -- Close the file
        try
            close access file theFile
        end try

        -- Return a boolean indicating that writing failed
        return false
    end try
end writeTextToFile



on getTimeInHoursAndMinutes()
    -- Get the "hour"
    set timeStr to time string of (current date)
    set Pos to offset of ":" in timeStr
    set theHour to characters 1 thru (Pos - 1) of timeStr as string
    set timeStr to characters (Pos + 1) through end of timeStr as string

    -- Get the "minute"
    set Pos to offset of ":" in timeStr
    set theMin to characters 1 thru (Pos - 1) of timeStr as string
    set timeStr to characters (Pos + 1) through end of timeStr as string

    --Get "AM or PM"
    set Pos to offset of " " in timeStr
    set theSfx to characters (Pos + 1) through end of timeStr as string

    return (theHour & ":" & theMin & " " & theSfx) as string
end getTimeInHoursAndMinutes

I expect when I run this to get an output of true and for my file to contain a new line of the current time. However it currently returns false with nothing written to the file.


Solution

  • The problem is that your script is using a file specifier in the open for access line, where it should use a POSIX file specifier (because you're feeding the command a POSIX path). It should look like this:

    writeTextToFile(getTimeInHoursAndMinutes(), "/Users/labspecialist/Desktop/system_log")
    
    on writeTextToFile(theText, theFile)
        try
    
            -- Convert the file to a string
            set theFile to theFile as string
    
            -- Open the file for writing
            set theOpenedFile to (open for access POSIX file theFile with write permission)
    
            -- Write the new content to the file
            write theText to theOpenedFile starting at eof
    
            -- Close the file
            close access theOpenedFile
    
            -- Return a boolean indicating that writing was successful
            return true
    
            -- Handle a write error
        on error errstr
            display dialog errstr
            -- Close the file
            try
                close access file theFile
            end try
    
            -- Return a boolean indicating that writing failed
            return false
        end try
    end writeTextToFile
    

    P.s. And yes, you really should use that opening slash in the file path, though it seems to work regardless...