Skip navigation

Run an applescript at certain time?

6839 Views 44 Replies Latest reply: Dec 14, 2013 7:38 AM by dickguertin RSS
  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Feb 19, 2013 8:31 AM (in response to MacMan240)

    Here's the eternal help-desk misunderstanding.  You don't know how to approach the problem but have all sorts of information about what your specific problem is.  We know how to approach problems like this, but have very little information about your specific needs.  Help occurs when we bridge that gap. 

     

    We need to know things like the following:

    • what folder do you want things deleted from?
    • what kinds of files are the files to be deleted (extension, file type, uti, or etc)?
    • what pattern of file names do you want deleted (prefixes, suffixes, keywords, or etc)?

    Don't be shy about spelling things out in detail; we like that!

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Feb 19, 2013 4:58 PM (in response to MacMan240)

    so do you want to delete all pdf files from the desktop, or just pdf files with a numeric name?

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Feb 19, 2013 6:25 PM (in response to MacMan240)

    since you have control of the scanner, can you add an identifying label to the name?  If the file names were something like "Temporary Scan 001.pdf" that would make life easier.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Feb 19, 2013 6:48 PM (in response to MacMan240)

    Eh, never mind, I think this will work:

     

    tell application "System Events"

      -- find all pdf files that were created in the last 24 hours

              set questionableFiles to files of desktop folder whose type identifier is "com.adobe.pdf" and creation date > ((current date) - 1 * days)

      -- loop through files, deleting files whose name is all numbers

              repeat with thisFile in questionableFiles

                        set mainName to text 1 thru -5 of (get name of thisFile)

                        try

      -- if the file name has any non-numbers, this will throw an error and skip the delete

                                  mainName as integer

                                  delete thisFile

                        end try

              end repeat

    end tell


    save this as a script file somewhere convenient, then change the plist I gave above to read as follows (all that's changed is the ProgramArguments key, which now will point to an external file rather than build the script internally):

     

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

    <plist version="1.0">

    <dict>

              <key>Label</key>

              <string>user.delete.daily</string>

              <key>ProgramArguments</key>

              <array>

                        <string>osascript</string>

                        <string>/path/to/script file.scpt</string>

              </array>

              <key>StartCalendarInterval</key>

              <dict>

                        <key>Hour</key>

                        <integer>0</integer>

                        <key>Minute</key>

                        <integer>0</integer>

              </dict>

    </dict>

    </plist>

  • Frank Caggiano Level 7 Level 7 (22,830 points)
    Currently Being Moderated
    Feb 19, 2013 6:52 PM (in response to MacMan240)

    MacMan240 wrote:

     

    Numerical name only please, the paramedics also have instructional pdfs on the desktop as well.

    Oh man you really need to take a step back and set this up in a more sane manner. Having both temporary files and permanent files in the same folder (and having that folder be the Desktop) with the same file extension is not a good thing. At some point some or all of the instructional pdfs will get clobbered.

     

    Why not at least set up two folders one for the instructional pdfs and one for the temporary ones. It will make your life a lot easier in the long run.

  • Frank Caggiano Level 7 Level 7 (22,830 points)
    Currently Being Moderated
    Feb 19, 2013 6:55 PM (in response to twtwtw)

    - find all pdf files that were created in the last 24 hours

    Shouldn't you be looking for files older than a given time.

     

    If you get all files created in the last 24 hours what happens if a EMT loads a file right before this runs and it hasn;t been used yet?

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Feb 20, 2013 1:59 PM (in response to MacMan240)

    ok, then you can simplify the script I gave above to read so:

     

    tell application "System Events"

      -- find all pdf files that were created in the last 24 hours that start with face_sheet

              delete (files of desktop folder whose type identifier is "com.adobe.pdf" and creation date > ((current date) - 1 * days) and name starts with "face_sheet_")

    end tell

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Feb 20, 2013 2:21 PM (in response to MacMan240)

    what exactly are you trying to accomplish with this?  If it's just to keep from deleting files that are being worked on at the moment, a better solution would be this, where you offset deletion by an hour:

     

    tell application "System Events"

      -- find all pdf files that were created between 1 hour and 26 hours ago that start with face_sheet

              set lowerLimit to ((current date) - 1 * days - 2 * hours)

              set upperLimit to ((current date) - 1 * hours)

              delete (files of desktop folder whose type identifier is "com.adobe.pdf" and creation datelowerLimit and creation date < lowerLimit and name starts with "face_sheet_")

    end tell

     

    This won't touch any files newer than an hour old, but will catch them the next day if they're still there. (I used 2 hours on the lower limit for overlap, so you don't get some weird situation where a file is created exactly on the hour and never gets deleted).

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Feb 20, 2013 2:26 PM (in response to MacMan240)

    Thinking about overlaps, I should point out that if the computer happens to be off or asleep at midnight, the script will run the next time the computer's activated.  However, it only looks back a day and 2 hours; if the machine isn't reactivated until (say) 6am there's a four hour window where the script might miss files.  Probably not an issue, but...

Actions

More Like This

  • Retrieving data ...

Bookmarked By (3)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.