Moeen
Moeen

Reputation: 61

Add path to text

I have a repeat which have

   repeat with myrecordID in rtetr
            set refpdf to field "File Attachments" of record myrecordID
            if refpdf is "" then
                set noref to noref + 1
            else
                set refpdf2 to POSIX path of (docs & "/PDF" & refpdf1)
                set thePath to refpdf2
                set thePath1 to my convertPathToAlias(thePath)
                set thePath1 to thePath1 as text
            end if
            
        end repeat

variable is path to some files I want to write this variable to a text file & add each path to a line. but when I try

set myFile to open for access addtemp with write permission
        write namesText to myFile
        close access myFile

It only write the last variable, not all of them from repeat.

I also tried this

try
        set fileDescriptor to open for access addtemp with write permission
        write thePath1 & return to fileDescriptor
        close access fileDescriptor
    on error e number n
        try
            close access file addtemp
        end try
        display dialog "Error: " & e & " - number: " & n buttons {"Cancel"} default button "Cancel"
    end try

but no chance

Upvotes: 0

Views: 75

Answers (1)

Ted Wrigley
Ted Wrigley

Reputation: 3194

Ok it seems you are working with in a tell block for some application you haven't mentioned — set refpdf to field "File Attachments" of record myrecordID is not standard AppleScript, and won't compile on its own — but using a generic application name, I think you want something that looks like this (please remember to replace "Generic Name" with the name of the app in question):

try
    set myFile to open for access addtemp with write permission
on error
    close access addtemp
    set myFile to open for access addtemp with write permission
end try

tell application "Generic Name"
    repeat with myrecordID in rtetr
        set refpdf to field "File Attachments" of record myrecordID
        if refpdf is "" then
            set noref to noref + 1
            -- write out a notice that there was no data, with a line break after
            write "No Reference" & return to myFile
        else
            set refpdf2 to POSIX path of (docs & "/PDF" & refpdf1)
            set thePath to refpdf2
            set thePath1 to my convertPathToAlias(thePath)
            set thePath1 to thePath1 as text
            -- write out the data, with a line break after
            write thePath1 & return to myFile
        end if
    end repeat
end tell
close access myFile

In short, you open the write-out file at the start of the script, write the data sequentially inside the repeat loop, and then close the file at the end.

The try block at the beginning handles a frequent problem: if your script errors out before the close statement, the file handle is left open and can error if you try to open it again; this try block detects an error, and tries to close and reopen the file handle.

You may need to put the keyword my before the write statements if the app in question defines its own write command.

Upvotes: 1

Related Questions