Applescript: Daily Folder Check to add/remove time-sensitive items

Discussion in 'Mac Programming' started by hideflomein, Oct 20, 2014.

  1. hideflomein, Oct 20, 2014
    Last edited: Oct 20, 2014

    hideflomein macrumors newbie

    Joined:
    Oct 20, 2014
    #1
    I handle the digital signage for a university and I currently have an older Mac Pro set up to show a screen saver drawing from a folder of images I sync with DropBox. These images show upcoming events, student notices, a welcome splash screen etc.

    I am trying to write a script (or scripts) that will do two things daily:

    1) Check a "Slides to Rotate In" folder for a start date in each filename, and if the date found is past the current date, it would move the file to the "Current Slides" folder. After moving all necessary files, it would stop and restart the slideshow.

    2) Check the "Current Slides" folder for an end date in each filename, and if the end date is past the current date, it would move the file to the "Old Slides" folder. Same deal with the screen saver afterwards.

    The files are named as such "002-CH10-20141013-20141023-common hour oct 20.jpg", where 002-CH10 is a category and identifier, 20141013 is the date that slide should start running, 20141023 is the date that the slide should be removed from rotation, and the trailing section is just a descriptor for my own sake.

    Here is the code I have so far for number 1 up above (number 2 would be pretty much the same), kind of cobbled together from some other info I found online before coming here:

    Code:
    to extractStartDate(filename)
    	set TempID to AppleScript's text item delimiters
    	set AppleScript's text item delimiters to "-"
    	set fnParse to every text item of filename
    	set returnStartDate to text item 3 of fnParse
    	set AppleScript's text item delimiters to TempID
    end extractStartDate
    
    to date_format(old_date)
    	set {year:y, month:m, day:d} to result
    	set todayDate to (y * 10000 + m * 100 + d)
    end date_format
    
    set old_date to current date
    set todayDate to date_format(old_date)
    -- return todayDate
    
    set filez to "/Users/<username>/Dropbox/Music School/Lobby Slides/Slides to Rotate In/"
    set dropBox to "/Users/<username>/Dropbox/Music School/Lobby Slides/Current Slides/"
    tell application "System Events" to set theFolderList to the name of every file of folder filez whose visible is true
    
    repeat with fa from 1 to count of theFolderList
    	set fn to text item fa of theFolderList
    	-- return fn
    	set addDate to extractStartDate(fn)
    	-- return addDate
    	if addDate comes before todayDate then
    		tell application "Finder" to move fn to folder dropBox
    	end if
    end repeat
    
    tell application "System Events"
    	stop current screen saver
    	start current screen saver
    end tell
    EDIT: I commented out the various "return" commands and got it to progress through to the point where it tells the Finder to move the file to the new folder. I think that's my only stumbling point right now. Any thoughts?
     
  2. hideflomein, Oct 20, 2014
    Last edited: Oct 20, 2014

    hideflomein thread starter macrumors newbie

    Joined:
    Oct 20, 2014
    #2
    Fixed it! And added the second part of what I wanted to do. Only issue is it throws up "error number 0" for each file it moves. Any way to avoid that?

    Code:
    to extractStartDate(filename)
    	set TempID to AppleScript's text item delimiters
    	set AppleScript's text item delimiters to "-"
    	set fnParse to every text item of filename
    	set returnStartDate to text item 3 of fnParse
    	return returnStartDate
    	set AppleScript's text item delimiters to TempID
    end extractStartDate
    
    to extractEndDate(filename)
    	set TempID to AppleScript's text item delimiters
    	set AppleScript's text item delimiters to "-"
    	set fnParse to every text item of filename
    	set returnEndDate to text item 4 of fnParse
    	return returnEndDate
    	set AppleScript's text item delimiters to TempID
    end extractEndDate
    
    to date_format(old_date)
    	set {year:y, month:m, day:d} to result
    	set todayDate to (y * 10000 + m * 100 + d)
    end date_format
    
    set old_date to current date
    set todayDate to date_format(old_date)
    
    set fileLocationRotateIn to "/Users/<username>/Dropbox/Music School/Lobby Slides/Slides to Rotate In/"
    set currentSlides to POSIX file "/Users/<username>/Dropbox/Music School/Lobby Slides/Current Slides/"
    set currentSlidesRotateOut to "/Users/<username>/Dropbox/Music School/Lobby Slides/Current Slides/"
    set discardedSlidesLocation to POSIX file "/Users/<username>/Dropbox/Music School/Lobby Slides/Old Slides/"
    
    tell application "System Events" to set theFolderList to the name of every file of folder fileLocationRotateIn whose visible is true
    repeat with fcount from 1 to count of theFolderList
    	set fileInstance to text item fcount of theFolderList
    	set addDate to extractStartDate(fileInstance)
    	set fnFull to POSIX file {fileLocationRotateIn & fileInstance}
    	if addDate comes before todayDate then
    		tell application "Finder" to move fnFull to currentSlides
    	end if
    end repeat
    
    tell application "System Events" to set theFolderList to the name of every file of folder currentSlidesRotateOut whose visible is true
    repeat with fcount from 1 to count of theFolderList
    	set fileInstance to text item fcount of theFolderList
    	set removeDate to extractEndDate(fileInstance)
    	set fnFull to POSIX file {currentSlidesRotateOut & fileInstance}
    	if removeDate comes before todayDate then
    		tell application "Finder" to move fnFull to discardedSlidesLocation
    	end if
    end repeat
    
    tell application "System Events"
    	stop current screen saver
    	start current screen saver
    end tell
     
  3. hideflomein thread starter macrumors newbie

    Joined:
    Oct 20, 2014
    #3
    So I've pretty much figured out the code for myself (except for the "error number 0" on each file move). Now to implement it on a daily basis - would it be best to do this via an iCal alarm?
     

Share This Page