How to close all files that an Applescript has opened?

I'm writing a script with multiple "open for access file ... with write permission" commands. It writes to various of them, making csv databases as it works its way through analyzing an xml file (generated by other software), then closes them all at the end.
My problem is that, if the script crashes or I have to halt it before it gets to the "close" commands at the end, the files are left open. This causes an obvious error when next running the script after making adjustments.
How can I ensure that those files are always closed? At present I'm doing it by quitting and re-starting Script Editor, which leaves me disoriented in the code.
Is there a generic close command that I can insert at the start of the script, for example?

Use this handler to open files.  It tries to open the file, and if it gets error -49 (file already open) it closes the file and reopens it.  It returns a file pointer to the open file, which you can use to refer to the file later.
on openAFile(filePath, writable)
          try
                    set fp to open for access filePath write permission writable
          on error errstr number errNum
                    if errNum = -49 then
                              close access filePath
                              set fp to open for access filePath write permission writable
                    else
                              display dialog errstr
                              return false
                    end if
          end try
          return fp
end openAFile
set pointer to openAFile("/path/to/some/file", true)
set fileContents to read pointer
--close access pointer

Similar Messages

  • RV042 - How to close all ports and leave some specific open

    Hello everybody,
    Here is the scenario of my network:
    - A company with 20 branches in Rio de Janeiro area. The main servers are in a datacenter located in downtown.
    Each branch has a RV042 router with firmware version 1.3.12.19-tm (Feb 13 2009 13:03:21) installed.
    All users in this network have a proxy configuration pointing to proxy.[blah].com.br port 3128.
    The HTTP/HTTPS traffic should go through proxy only.
    The network settings for every RV042 are similar:
    RV042 LAN IP = 172.16.***.1 /24.
    RV042 WAN IP = 192.***.***.*** /30.
    Network Setting Status
    LAN IP: 172.16.***.1 /24
    WAN1 IP: 192.168.***.*** /30               
    WAN2 IP: Not used
    Mode: Router
    DNS(WAN1): 208.67.220.220 / 208.67.222.222 [OpenDNS Service]
    DNS(WAN2): Not used
    Firewall Setting Status
    SPI (Stateful Packet Inspection): On
    DoS (Denial of Service): On
    Block WAN Request: On
    Firewall -> Access Rules Section: Please see below
    The problem:
    - Some "smart" users were caught using Ultrasurf application, which changes the proxy settings to go through port 9666 or even 443.
    In other machines, we've found some black proxies [for example: 212.46.27.142 port 8080].
    My objective:
    - To close all ports in Firewall -> Access Rules section and grant permission only to some selected and specified ports.
    - To redirect all HTTP/HTTPS connections to go to proxy's IP address only.
    Gentlemen, could you please tell me which Access Rules can I set in these RV042s in order to block and prevent these users to continue abusing this network? Is there anything else am I missing?
    P.S.: The users who were caught using Ultrasurf were fired. ;-)
    I gladly appreciate your comments.
    Thanks in advance,
    Luciano

    Hello Randy,
    Thanks for your answer. But let me tell you better. In this scenario, all machines in LAN can use RDP and VNC normally.
    I want to block everything else, just to avoid users to try using other ports.
    You might want to try Deny all traffic out, except for the PC's you need  to use RDP, and then only allow 80, and 443 to the proxy.[blah].com.br  port 3128. This way all web traffic would have to pass through that  proxy, otherwise it would be denied.
    In this network, we use VNC to provide remote IT support to users in these branches.
    They use RDP to access some servers in the datacenter.
    I think that the rule below line 6 is letting users go and traverse the proxy.
    Priority    Policy Name    Enable    Action    Service        Source Iface    Source    Destination        Time    Day
    *                                                  Allow    All Traffic [1]             LAN     Any        Any                  Always    
    How can I modify this access rule? It seems it cannot be altered.
    Thanks in advance,
    Luciano

  • View all files that have not been opened for a while?

    I'm trying to clean up my computer and make some space
    Problem is I just have too many files folders and subfolders so going in and out of each folder will take way too long
    So my question is
    Is there a search method that will show me all the files that I haven't used or opened in a while so that I could just browse through those?

    omsaj wrote:
    Is there a search method that will show me all the files that I haven't used or opened in a while so that I could just browse through those?
    Yep, sure is.
    Download the free Easy Find (direct download)
    https://s3.amazonaws.com/DTWebsiteSupport/download/freeware/easyfind/4.9/EasyFin d.app.zip
    Now what your going to do is select your Home folder or a folder inside say Documents
    Then your going to adjust your search settings on the left and use the letter "a" as the search term and click the search and let it rip, it will take some time.
    Next click sort by the "Modified" date and you can then go about it
    Repeat for the letter "e" "i" "o" "u"
    Be careful, Easy Find also finds invisible and hidden files, and you dont' want to delete the iPhoto Libray or anything in the hidden Users/Library folder.
    Perhaps you should create a backup first, then later on if you made a mistake you can recover
    Most commonly used backup methods

  • How to delete all "files that cannot be found" files faster! Desperate!!

    Hey!! So for the past year and a half, i have had hundreds and hundreds of songs that read "----- file cannot be found--" due to me listening to cd's through itunes, without properly importing them. The result was every song file was saved without the song! So now after i imported the CD's correctly, i have quite a few duplicates of hundreds of songs, and the only way i find out which song is the duplicate is by manually trying to play the individual song. I wish to DELETE all the BLANK songs whether they be duplicates or just single files. I was just tolerating it all this time, but now that i ordered my itouch, i need to consolidate my library! Please help, thanks!!
    PS: I saw other threads where people wanted to delete duplicate songs, but i didn't know if the programs that where reccomended to them deleted a random duplicate, because if that is the case, it could delete the good copy of the song, where i would want it to delete the blank copy. Thanks again.
    = ]
    THANKS AGAIN!!! PLEASE HELP THIS PROBLEM IS KILLING ME! =O
    Message was edited by: mr.nooby

    Here is a script to find dead tracks.
    Rename it to FindDeadTracks.js
    Run iTunes.
    While iTunes is running, double click FindDeadTracks.js to run the script.
    http://ottodestruct.com/itunes/FindDeadTracks.txt
    You can look at the resulting Notepad document it creates, if you like, to make sure it found files you were expecting.
    Then to get rid of those tracks, download this: http://ottodestruct.com/iTunes/RemoveDeadTracks.txt
    Rename it to RemoveDeadTracks.js.
    Run iTunes.
    While iTunes is running, double click RemoveDeadTracks.js to run the script. It'll remove all the exclamation marked tracks.
    For the duplicate tracks, you pretty much need to look at those on a case-by-case basis.

  • How to move all files but X to Y

    It doesn't really matter if it's in the terminal or if it's a applescript but I need to know how to:
    Move all files in a dictionary but a few named.

    Could you pleas explain more?
    If you do not understand the shell while loop, it is possible that using shell commands is a bit more dangerous than you might want to try.
    The explanation:
    ls -a | grep -v "X" | while read file
    do
        mv "$file" "Y/"
    done
    ls -a will list all the files in the current working directory (including the invisible files that start with a leading period.  Thinking about this, maybe you do not want to include -a.  You could cd /path/to/the/desired/directory as a way to set your current working directory as the directory you wish to move things from
    cd "/path/of/the/from/directory"
    ls | grep -v "X" | while read file
    do
        mv "$file" "Y/"
    done
    The ls command's output is piped (the vertical bar | ) into the grep command (general regular expression parser).  The -v option tells grep to throw away any lines (filenames) that match the regular expression "X".  So if the regular expression X matches all the files you do not want to move, the filenames send to the next pipe will be the files you wish to move.
    The while loop will read the filenames grep has selected and invoke the mv (move) command for each file selected by grep and move them to the destination directory Y
    The bigest risk is that you will execute this in the wrong directory and move the files that you need somewhere else.  OR WORSE, if "Y" is NOT a directory, and you DO NOT put a trailing / the mv could move easy file to the destination file, deleting the previous file for each file moved, so effectively deleting every file in the directory except the last one.
    This is why I said if you do not understand what is going on with the shell commands, you could do damage to your system.
    ls could have the /path/of/the/from/directory instead of using the cd command.  The grep command could be replaced with egrep that allows using alternate matching strings:
    ls /path/of/the/from/directory | egrep -v "abc.txt|def.jpg|xyz.mp3|etc..." | while read file
    do
        mv "$file" "/path/to/the/destination/directory/"  # notice the trailing slash for safety
    done
    Good luck.  I hope you do not destroy your system

  • How to play video files that are in Drop box on iPod?

    How to play video files that are in Drop box on iPod?

    I am going to have to write a technote about this, because it comes up a lot.
    Flash Player itself is designed to play SWF content as a browser plugin or as an ActiveX control (when it's IE Windows).
    There is a standalone Flash Player, but that's something Adobe  provides directly as a feature of Flash CS4 Professional. It gets installed with Flash. We have made some standalone players available on http://www.adobe.com/support/flashplayer/downloads.html, but not all (and I don't know why.  Looking into it)
    There are numerous 3rd party standalone SWF players out there though.  Some designed to just play video, others that play full SWF. I suggest a google search of 'play SWF standalone' and read the results.
    On the Mac, consumers who want to play SWF content off their desktop can use the Adobe Media Player. You can import a local SWF (and FLV and other video) and play it in AMP.  Get it here:
    http://www.adobe.com/products/mediaplayer/
    That's Mac only, doesn't work on Windows.

  • How to select all files in "user dir" (LastAccessedDirectrory) using applet

    Hi,
    Can anyone tell me " How to select all files in "user dir" (Last Accessed Directrory) using applet"?
    For this which method in applet used?
    Thanks in Advance.

    1) Do you want "user dir" (watever that is) or do you wnat the last accessed directory? Last accessed by what/whom?
    2) Applets can't access the local file sytem, and for good reason, unless they're signed and the client has given permission.

  • File Dialog - How to remove all files pattern in LV 8.x

    How to remove "all files" pattern in LV 8.x in the file dialog.
    Is it possible?
    Thanks
    Dany
    Dany Allard

    If it is a security issue, you will probably need to implement a custom dialog and actively filter for file types.  No matter what pattern you have showing, the user can type a pattern into the dialog and get anything they want.  In addition, file extension is not sufficient to determine file type.  You should probably check the file itself to ensure it is an HTML file and does not have any embedded nastiness.
    Fortunately, custom dialogs are easy to make.  Using the event structure, you can filter inputs as well, so that only HTML files will be accepted.  Good luck.  Let us know if you need more help.
    This account is no longer active. Contact ShadesOfGray for current posts and information.

  • How to close all alerts?

    Hello ,
    when i open plug in and perform opration then alert window show. i want to close that alert on menu item click.how to close all alerts on button or menu item
    click?can any body help me?

    Double click the home button.  Swipe up toward the top of the iPhone each icon that you see over an app icon.
    When finished, single clock the home button.
    This closes each application one at a time.
    To close them all together, restore the iPhone.

  • How do I restore files that were backed up by Adobe?

    How do I restore Files that were backed up by Adobe?

    Where is the back-up e.g. on removable storage such as CD, DVD or external hard drive?
      If the files are on Photoshop.com, then Elements can sync them back down, by following these steps:
    Launch the Elements program.
    Login to your photoshop.com account
    Open Organizer
    On the menu click Edit à Preferences à Backup/Synchronization
    You should see all of your albums and media files on your photoshop.com account.
    Mark those checkboxes that you want to restore to your computer.
    Click OK.
    N.B. Elements can only sync files back down to your hard drive which reside in albums on Photoshop.com

  • Cannot download application world- how to close all the browser windows

    i cannot download the blackberry application world "sorry, your session has expired. Please close all of your browser windows and then return to the welcome page to start again." How to close all the browser windows?

    Hello numadia
    Welcome to BlackBerry Support Forums
    Is your problem resolved or you're still having problem with download BlackBerry world !
    If you're having problem then just peform a Battery pull restart by removing the battery while your device is Powered On, wait for a min. then reinsert it back after reboot please try it again.
    If problem persists then you can also go through this Knowledge Base  KB32113
    Click " Like " if you want to Thank someone.
    If Problem Resolves mark the post(s) as " Solution ", so that other can make use of it.

  • HT1660 how can i view files that i buck up from my old device to i tunes

    how  can you view file that i buck up from old divice to itunes

    Reset and Erase before selling
    Settings>General>Reset>Erase all Content and Settings

  • How to close all applications at once in an iPhone ?

    how to close all applications at once in an iPhone ?

    Double click the home button.  Swipe up toward the top of the iPhone each icon that you see over an app icon.
    When finished, single clock the home button.
    This closes each application one at a time.
    To close them all together, restore the iPhone.

  • How can I access files that I moved from an older MacBook Pro to a newer one via Firewire and Migration assistant.  The files show up on the new MacBook but cannot be opened.  Thanks!

    How can I access files that I moved from an older MacBook Pro to a newer one via Firewire and Migration assistant?  The files show up on the new MacBook but cannot be opened.  Thanks!

    Get info then check permissions then add your curent user name (it was probably different on old Mac) and give your username full read/write permissions.

  • How to retrieve all datas that lost when i update my iphone4 to that 6.10 ios. i tried to look at may i tunes in my computer but it seems that i forgot to perform back ups since i purchased this phone 2yirs ago.. pls send me an advice..thank u.

    how to retrieve all datas that lost when i update my iphone4 to that 6.10 ios. i tried to look at may i tunes in my computer but it seems that i forgot to perform back ups since i purchased this phone 2yirs ago.. pls send me an advice..thank u.

    All of the data should be on your computer, simply sync it back.
    If the update was done via iTunes on the computer, the first step in the process is a backup of the device.

Maybe you are looking for

  • Report this postReply with quote Credit check for unconfirmed item

    Hello, I have configured automatic credit control for sales orders. When a confirmed order quantity is entered, I get a warning message plus the credit block, just as I need. If I have no confirmed quantity of the items, I get no message, just a save

  • Bringing out colors on a black and white photos

    I just got Photoshop elements 11 for Xmas. I've been attempting to have a single color or object colored in a black and white picture. I have gotten as far as making a duplicate layer and then creating a mask but don't know where to go from here. How

  • When to backup the rollback segments?

    Does anyone know when do I need to backup the rollback segments and what for? How are they used during restore/recovery? I'm using RMAN in Oracle 9.2. We have databases with both manual and automatic undo management. Does the undo management type mak

  • Unix and grep

    I have the standard 10.6.4 installation and I'm using the Unix command line. I am experiencing anomalies with grep. According to grep -V, I have version 2.5.1. The man page identifies this as gnu grep, and says it is identical in functionality to egr

  • Music Videos worse with every iTunes update

    I have purchased a number of music videos over the years. In the past they played fine on Windows XP. With every iTunes update, they've gotten more unstable, beginning with the inability to preview videos on iTunes. (The audio worked, but the image w