PDA

View Full Version : In need of scripting advice




ebobster
May 15, 2013, 03:31 AM
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



kryten2
May 15, 2013, 09:26 AM
From StefanK on macscripter.net :

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 (http://macscripter.net/viewtopic.php?id=40134)

Adding to folder Folder Action handler :

Info : Folder Actions Reference (https://developer.apple.com/library/mac/#documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_folder_actions.html#//apple_ref/doc/uid/TP40000983-CH219-SW48)

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 (http://hints.macworld.com/article.php?story=20061104095603502) and Use Automator to keep the desktop clean (http://hints.macworld.com/article.php?story=20060130010025791)

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

--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 :

<?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>