iosobjective-cexcelapplescriptapplescript-objc

Applescript: "File Already Open" when writing to new file


I'm creating a text file whose file name will consist of constant and variable strings. For whatever reason, I'm getting an error saying "[file name] is already open" when I'm actually just creating it. The file is created, but none of my content makes it into the file.

All of the fixes I've tried end in another error saying "network file permission."

Also, I should mention that my new file is going into the same container as another file that is used to create the new file, which is where the filePathAlias comes in.

Any ideas? Thanks in advance!

Here's the script:

-- get the file --
set filePathAlias to (choose file with prompt "** Choose File **")
set filePath to filePathAlias as string

tell application "Finder"
    set fileName to name of filePathAlias
    set containerPath to (container of filePathAlias) as string
end tell

set filePath to filePathAlias as string

-- get file container --
tell application "Finder"
    set containerPath to (container of filePathAlias) as string
end tell

-- combine file name variable with constant suffix --
set finalFile to locationName & "_RMP_2014.txt"

-- create the file (THIS IS WHERE THE ERROR COMES IN) --
set myFile to open for access (containerPath) & finalFile with write permission
set listTextFinal to "text goes here"
try
    write listTextFinal to myFile as text
    close access myFile
on error
    close access myFile
end try

Solution

  • You didn't give an example path for filePathAlias or locationName. I was unable to reproduce the file already open error. I can reproduce the network file permission error...So:

    set filepathalias to ((path to desktop folder as string) & "test" as string) as alias
    --alias of folder on desktop called test... massaged well to be an alias that can later be converted to string when making containerPath
    
    set locationName to "stuff you left out" --Just a name I assume...
    
    -- get file container --
    tell application "Finder"
        set containerPath to ((container of filepathalias) as string)
    end tell
    
    -- combine file name variable with constant suffix --
    set finalFile to locationName & "_RMP_2014.txt"
    
    -- create the file (THIS IS WHERE THE ERROR COMES IN) --
    set myFile to open for access (containerPath) & finalFile with write permission
    set listTextFinal to "text goes here"
    try
        write listTextFinal to myFile as text
        close access myFile
    on error
        close access myFile
    end try
    

    This works perfectly, if you were to be working to the desktop. The problem appears to be in the stage of getting the path correct.

    Without all the massaging to the filepathalias I did in the first line we get the network file error. The file is trying to save in places you can not save to....

    You will need to verify the filepathalias, containerPath, & finalFile all contain the information you'd expect.

    Right below where the finalFile is set try this from the editor:

    return {filepathalias as string, containerPath as string, finalFile as string}
    

    my result from the above:

    {"mac:Users:lithodora:Desktop:test:", "mac:Users:lithodora:Desktop:", "stuff you left out_RMP_2014.txt"}
    

    That is similar to what you should expect.