Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 15, 2013, 03:31 AM   #1
ebobster
macrumors newbie
 
Join Date: Apr 2011
In need of scripting advice

Hi,

bout to teach myself applescript and bought the developers reference manual and before I plunge into it I'd love to know if my script structure makes sense. I'm not a real hercules-esque coder, but I know several languages quite well for work, etc...

what I want to do is have files of various different MIME formats (jpg, png, mov, mp4, etc.) move moved into specified folders automatically. To start I want to have my Desktop actively move files into specified folders based on their MIME type. so images go into the ~/Desktop/Media/Images directory; movies into the movies folder within the /Media folder. You get the idea.

my outline is as follows:

First define all parent folders and subdirectories and specify which MIME type each folder is linked with. (create folders before scripting).

tell finder to scan desktop folder with some sort of IF loop that scans for filetypes based on either MIME type or file extension.

tell finder app to move the recognized filetypes into their requisite folders.

End script...

result should be that it cleans up everything and moves the files on my crowded desktop into folders so they can be easily found. If you have any tips from personal experience would love to hear them so i can learn from others who know much more about applescript.

one thing I'm not clear on is how I can have the Desktop folder sort files by simply putting an image file on the desktop and then having it moved automatically to the images folder. I'd rather not have to run the script everytime I need to organize files. If this is within the realm of "all-nighter script marathon" I'm in, but if It will require a buttload of code and days of digging into the applescript dev manual then screw it.

Cheers
ebobster is offline   0 Reply With Quote
Old May 15, 2013, 09:26 AM   #2
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
From StefanK on macscripter.net :

Quote:
You could attach a folder action to the desktop folder, but for performance reasons it's not recommended to monitor the desktop folder via folder action.
Info : Clean up desktop by kind

Adding to folder Folder Action handler :

Info : Folder Actions Reference

Code:
on adding folder items to this_folder after receiving these_items
	-- insert actions here
end adding folder items to
Examples : An AppleScript to tidy up the Desktop and Use Automator to keep the desktop clean

To keep my Downloads folder clean I use this script with launchd :

Code:
--Move downloaded files to subfolders launchd script
--Adapted to run with launchd by removing the on adding
--handler and some other modifications
--on adding folder items to this_folder after receiving these_items
-- insert actions here
set ExtensionsList to {"dmg", "doc", "exe", "nzb", "pdf", "pkg", "mpkg", "rar", "srt", "idx", "sub", "zip"}
set theDownloadsFolder to (path to "down")
tell application "System Events"
	set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
end tell
set Growl_Title to "Finder Move"
if folderReady(theDownloadsFolder) then
	tell application "Finder" to set these_items to (every file of theDownloadsFolder whose name extension is in the ExtensionsList) as alias list
	repeat with i from 1 to number of items in these_items
		set this_item to item i of these_items
		-- insert actions here
		--			tell application "Finder" to set this_item_ext to this_item's name extension
		tell application "Finder"
			set this_item_ext to this_item's name extension
			if this_item_ext is "dmg" then
				-- item 1 action goes here
				move this_item to (theDownloadsFolder as string) & "DmgFiles"
				if isRunning then
					my sendGrowl("Moved file", Growl_Title, "Moved " & this_item's name & " to subfolder DmgFiles")
				end if
			else if this_item_ext is "doc" then
				-- item 2 action goes here
				move this_item to (theDownloadsFolder as string) & "DocFiles"
				if isRunning then
					my sendGrowl("Moved file", Growl_Title, "Moved " & this_item's name & " to subfolder DocFiles")
				end if
			else if this_item_ext is "exe" then
				-- item 3 action goes here
				move this_item to (theDownloadsFolder as string) & "ExeFiles"
				if isRunning then
					my sendGrowl("Moved file", Growl_Title, "Moved " & this_item's name & " to subfolder ExeFiles")
				end if
			else if this_item_ext is "nzb" then
				-- item 4 action goes here
				move this_item to (theDownloadsFolder as string) & "NzbFiles"
				if isRunning then
					my sendGrowl("Moved file", Growl_Title, "Moved " & this_item's name & " to subfolder NzbFiles")
				end if
			else if this_item_ext is "pdf" then
				-- item 5 action goes here
				move this_item to (theDownloadsFolder as string) & "PdfFiles"
				if isRunning then
					my sendGrowl("Moved file", Growl_Title, "Moved " & this_item's name & " to subfolder PdfFiles")
				end if
			else if this_item_ext is "pkg" or this_item_ext is "mpkg" then
				-- item 6 action goes here
				move this_item to (theDownloadsFolder as string) & "PkgFiles"
				if isRunning then
					my sendGrowl("Moved file", Growl_Title, "Moved " & this_item's name & " to subfolder PkgFiles")
				end if
			else if this_item_ext is "rar" then
				-- item 7 action goes here
				move this_item to (theDownloadsFolder as string) & "RarFiles"
				if isRunning then
					my sendGrowl("Moved file", Growl_Title, "Moved " & this_item's name & " to subfolder RarFiles")
				end if
			else if this_item_ext is "srt" then
				-- item 8 action goes here
				move this_item to (theDownloadsFolder as string) & "SrtFiles"
				if isRunning then
					my sendGrowl("Moved file", Growl_Title, "Moved " & this_item's name & " to subfolder SrtFiles")
				end if
			else if this_item_ext is "idx" or this_item_ext is "sub" then
				-- item 9 action goes here
				move this_item to (theDownloadsFolder as string) & "SubFiles"
				if isRunning then
					my sendGrowl("Moved file", Growl_Title, "Moved " & this_item's name & " to subfolder SubFiles")
				end if
			else if this_item_ext is "zip" then
				-- item 10 action goes here
				--move this_item to (theDownloadsFolder as string) & "ZipFiles"
				my FolderMove(this_item, (theDownloadsFolder as string) & "ZipFiles")
				if isRunning then
					my sendGrowl("Moved file", Growl_Title, "Moved " & this_item's name & " to subfolder ZipFiles")
				end if
			end if
		end tell
	end repeat
end if
--end adding folder items to

on FolderMove(an_Item, Dest_Folder)
	tell application "Finder" to move an_Item to Dest_Folder
end FolderMove

on sendGrowl(growlName, growlTitle, growlDescription)
	tell application "GrowlHelperApp"
		(*
		-- Make a list of all the notification types 
		-- that this script will ever send:
		--only one notification
		set the allNotificationsList to {"Moved file"}
		-- Make a list of the notifications 
		-- that will be enabled by default.      
		-- Those not enabled by default can be enabled later 
		-- in the 'Applications' tab of the growl prefpane.
		--only one notification
		set the enabledNotificationsList to {"Moved file"}
		-- Register our script with growl.
		-- You can optionally (as here) set a default icon 
		-- for this script's notifications.
		register as application 
			"Move Downloaded Files To SubFolders" all notifications allNotificationsList 
			default notifications enabledNotificationsList 
			icon of application "Finder"
		
		
		
		set stickNotification to "no"
		if growlName is not equal to "Request Send" then
			set stickNotification to "yes"
		end if
		
		
		
		*)
		--	Send a Notification...
		notify with name growlName 
			title growlTitle 
			description growlDescription 
			application name "Move Downloaded Files To SubFolders" --sticky stickNotification
		
	end tell
end sendGrowl

on folderReady(tFolder)
	set myFolder to tFolder as alias -- use the path to the folder that's loading
	set firstSize to size of (info for myFolder) --get initial size
	delay 5 --wait 5 seconds
	set newSize to size of (info for myFolder) --get a newer size, bigger
	repeat while newSize ≠ firstSize --if they don't equal, loop until they do
		set firstSize to newSize --new base size
		delay 3 --wait three seconds
		set newSize to size of (info for myFolder) --get a newer size
	end repeat --once the sizes match, the transfer is complete
	return true
end folderReady
My com.test.downloadsmove.plist file :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>com.test.downloadsmove</string>
        <key>Program</key>
        <string>/usr/bin/osascript</string>
	<key>ProgramArguments</key>
	<array>
		<string>osascript</string>
		<string>/Users/test/Library/Scripts/Move downloaded files to subfolder launchd script.scpt</string>
	</array>
        <key>WatchPaths</key>
        <array>
        <string>/Users/test/Downloads</string>
        </array>
</dict>
</plist>
__________________
Space Corps Directive 34124

Last edited by kryten2; May 15, 2013 at 09:33 AM.
kryten2 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 12:04 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC