AppleScript Audio Filing Question

Discussion in 'Mac Programming' started by jaba813, Apr 10, 2010.

  1. jaba813 macrumors newbie

    Joined:
    Jun 9, 2009
    #1
    Hi,

    I would like to use Audio Hijack Pro to constantly record a police scanner. I have setup Audio Hijack Pro to record a new file every 30 minutes and to date and time stamp the files. I'm wondering how to run an AppleScript that would file all of that days files into a folder with the individual date at the end of the day.

    Essentially I would like to have the program record a new file every 30 minutes and at the end of the day take all the files with that day's date and move them into a folder denoting the date. Then have the process start over.

    Is that possible?

    Thanks,

    Chris
     
  2. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    jaba813 --

    I don't have Audio Hijack Pro, so I'll leave any issues with that to someone else. But here's a basic script for creating a folder named for the current date and some basic code for moving a file:

    Code:
    -- this is designed to give you the basics of what you need to know
    
    tell application "Finder"
    	
    	-- It would be great if we could just use "current date as string"
    	-- unfortunately, current date includes the time, which includes
    	-- colons which can't be part of a path name ...
    	
    	set dateRecord to (current date)
    	
    	-- so we pull out what we need individually from dateRecord
    	-- to build a proper date string that can used to name the
    	-- folder we're going to make
    	set dateString to ((((month of dateRecord) as integer) as string) & "-" & ((day of dateRecord) as integer) as string) & "-" & ((year of dateRecord) as integer) as string
    	
    	-- folder, of course doesn't have to be on the desktop; theLoc we'll
    	-- need shortly
    	
    	set theLoc to make new folder at (path to desktop) with properties {name:dateString}
    end tell
    
    -- we're going to use the Unix mv command to move a file from
    -- one place to another. We need to convert theLoc into unixese
    
    set thePosixLocation to (POSIX path of (theLoc as string))
    
    -- now we need a file. For this demo, we'll just let you pick one
    
    set aRandomFile to choose file
    
    set aRandomPosixFile to (quoted form of POSIX path of (aRandomFile as string))
    
    -- and here's mv
    
    set errMsg to do shell script "mv -iv " & aRandomPosixFile & " " & thePosixLocation
    
    -- this isn't necessary, but it will help you debug any issues.
    if errMsg is not "" then
    	display dialog errMsg
    end if
    
    mt
     
  3. jaba813 thread starter macrumors newbie

    Joined:
    Jun 9, 2009
    #3
    MT,

    Thanks for the quick reply. This is definitely what I was looking for. However, I'm completely clueless on modifying the script to fit within where I want the files to go. Is there anyway if I tell you a little more information you'd be able to help me customize the script to suit my needs? I would greatly appreciate it.

    I'd like to take the contents of the Audio Hijack folder and move the files at 12:00 Midnight.

    Path: /Users/charney/Music/Audio Hijack

    Thanks,

    Chris
     
  4. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #4
    Where do you want to put the files?

    Is there anything in the Audio Hijack folder that shouldn't be moved?

    Must it run at midnight?
     
  5. jaba813 thread starter macrumors newbie

    Joined:
    Jun 9, 2009
    #5
    Thanks for the reply. I really appreciate your help.

    The files can just go in the Audio Hijack folder, but I'd like them to place all the contents of that days files in a dated folder for that day at some point. However, I don't really care when it's done.

    The naming convention that Audio Hijack Pro is using for the files is as follows:

    20100410 19 22 07.m4a

    Thanks again in advance!

    Chris
     
  6. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #6
    I can't say this is 100 percent fool proof until you run it on your system. If I were you, I'd back up the audio files and see how this works.

    It works in your Audio Hijack folder. It only looks for m4a files, so if you have other files there, they should remain untouched. It creates folders based on the Audio Hijack naming convention: YYYYMMDD. You can run the script any time you like, and it should be able to clean up what's there. There is software that can run scripts at specific times during the day and you should be able to use this script as is.

    My biggest question mark is that I created a dummy folder with dummy m4a files to make this work, then replaced the path the dummy folder with the folder path you provided. It should work ... but still use backups until you're sure it does.

    No guarantees. Hope this works.

    mt

    Code:
    set workFolder to "Users::charney:Music:Audio Hijack:"
    
    
    tell application "Finder"
    	
    	-- The script needs the name of your hard drive; these first two lines provide it.
    	set tempString to ((path to users folder) as string)
    	set driveString to (characters 1 thru (offset of ":" in tempString) of tempString) as string
    	set fullPath to driveString & workFolder
    	
    	
    	-- set fileList to every file of alias ":Users:charney:Music:Audio Hijack:" whose name extension is "m4a"
    	
    	set fileList to every file of alias fullPath whose name extension is "m4a"
    	
    	repeat with fl in fileList
    		
    		repeat 1 times -- this double repeating allows for a graceful exit
    			set testString to word 1 of (name of fl as string)
    			try
    				set testNum to testString as integer
    				-- display dialog testNum
    			on error
    				exit repeat
    			end try
    			set targetFolder to driveString & workFolder & testString
    			set flPosix to quoted form of POSIX path of (fl as string)
    			
    			if exists alias targetFolder then
    				-- display dialog "Moving file; folder exists"
    				set targetPosix to quoted form of POSIX path of targetFolder
    				
    			else
    				-- display dialog "Creating folder"
    				set newFolderLoc to make new folder at alias (driveString & workFolder) with properties {name:testNum}
    				set targetPosix to (quoted form of POSIX path of (newFolderLoc as string))
    			end if
    			
    			set shellString to "mv -iv " & flPosix & " " & targetPosix
    			do shell script shellString
    			
    		end repeat
    		
    	end repeat
    	
    end tell
     
  7. jaba813 thread starter macrumors newbie

    Joined:
    Jun 9, 2009
    #7
    MT,

    This works perfectly. Thanks so much for all your hard work. I honestly have no clue about these kinds of things.

    One last question. When I run the script it prompts me to click OK for every file that the script moves into the folder. I was wondering if it's possible to automate that process? If not, it's not a big deal for me to run the script every so often and click OK through it. However, if you know of anyway that I could make this a more automated process (have the computer "Click OK" during folder creation) that would be great!

    Thanks again for all your help. I really appreciate it. I would have been completely lost without you pointing me in the right direction and writing the script.

    I have attached a screenshot for you to reference.

    Thanks again!

    Chris
     

    Attached Files:

  8. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #8
    I use "display dialog" as a kind of brute force debugging, and I accidentally left one behind.

    I fixed it in the original script. Just copy and paste it into AppleScript Editor and it should work fine.

    mt
     
  9. jaba813 thread starter macrumors newbie

    Joined:
    Jun 9, 2009
    #9
    MT,

    Perfect! Thanks so much again. I really appreciate it.

    Chris
     

Share This Page