• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Invisible File Generator V2
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Invisible File Generator V2


  • Subject: Re: Invisible File Generator V2
  • From: Christopher Stone <email@hidden>
  • Date: Mon, 04 Mar 2019 08:45:43 -0600

On 03/03/2019, at 19:57, Gil Dawson <email@hidden
<mailto:email@hidden>> wrote:
> I made some changes to conform to your helpful suggestions...


Hey Gil,

It's a good practice to always use an established handler to write files – one
that contains error-handling that at least tries to close the open file in case
of problems.

It's a pain in the neck to get saddled with a hanging open file.

Here are my two most used write-file handlers (writeUTF8 is used most
frequently).

--
Take Care,
Chris


set theText to "•• Now is the time for all good mén to come to the aid of their
country ••" & linefeed

writeFile(theText, "~/Downloads/Test 01.txt", eof, «class utf8»)

writeUTF8(theText, "~/Downloads/Test 02.txt")

------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------
on writeFile(srcData, targetFilePath, startPosition, dataType)
    try
        if targetFilePath starts with "~/" then
            set targetFilePath to POSIX path of (path to home folder as text) &
text 3 thru -1 of targetFilePath
        end if
        set fileRef to open for access targetFilePath with write permission
        if startPosition = 0 then
            set eof of fileRef to 0
        end if
        write srcData to fileRef as dataType starting at startPosition
        close access fileRef
    on error errMsg number errNum
        try
            close access fileRef
        end try
        error "Error in writeFile() handler of library: gen.lib" & return &
return & errMsg
    end try
end writeFile
------------------------------------------------------------
on writeUTF8(_text, targetFilePath)
    try
        if targetFilePath starts with "~/" then
            set targetFilePath to POSIX path of (path to home folder as text) &
text 3 thru -1 of targetFilePath
        end if
        set fRef to open for access targetFilePath with write permission
        set eof of fRef to 0
        write _text to fRef as «class utf8»
        close access fRef

        if targetFilePath contains ":" then
            return alias targetFilePath
        else if targetFilePath starts with "/" then
            return alias POSIX file targetFilePath
        end if

    on error e number n
        try
            close access fRef
        on error e number n
            error "Error in writeUTF8() handler of library: gen.lib" & return &
return & e
        end try
    end try
end writeUTF8
------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Invisible File Generator V2
      • From: Gil Dawson <email@hidden>
References: 
 >Invisible File (From: Gil Dawson <email@hidden>)
 >Invisible File Generator V2 (From: Gil Dawson <email@hidden>)

  • Prev by Date: Invisible File Generator V2
  • Next by Date: Re: Invisible File Generator V2
  • Previous by thread: Invisible File Generator V2
  • Next by thread: Re: Invisible File Generator V2
  • Index(es):
    • Date
    • Thread