Well I’ve gotten to 99 photo entries already and decided that this whole photolog thing is enough fun to put a bit more effort into is. So the photos now have there very own page!!! Sorddinville is online. And they also get there own RSS feeds now. Yay. Sorddinville XML. This also means I’ve got my applescript automatically uploading the files to the blog now. Which took a whole lot of sorting out. Still haven’t figured out how to get the script to publish the entries… i’ll work on that some other time. Oh, and in a side note, the reason the photos weren’t showing up was cause MovableType doesn’t put img tags in its ‘excerpt’ section, which is what it uses to make the feeds. By the way, the applescript that I’m using at the moment is in the extended entry if anyone really cares.
(*
Phogolog automation script
©2004 David Robinosn
This Folder Action script has only been tested with Mac OS X 10.3.4, use with eariler verison may not work.
This Folder Action handler is triggered whenever files are dropped into the attached folder. The script will process and organise the files for use on a photolog.
*)
--the folder for the photoblog images
property site_upload_foldername : "Publish"
--the folder for all of the images
property archive_foldername : "Archive"
-- the list of file types which will be processed
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"}
property type_list : {"JPEG", "TIFF", "GIFf", "PICT", "8BIM", "PNGf"}
-- since file types are optional in Mac OS X,
-- check the name extension if there is no file type
-- NOTE: do not use periods (.) with the items in the name extensions list
-- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"}
property extension_list : {"jpg", "jpeg", "tif", "tiff", "gif", "pct", "pict", "psd", "png"}
property dialog_timeout : 120 -- set the amount of time before dialogs auto-answer.
on adding folder items to this_folder after receiving these_items
-- CHECK FOR THE DESTINATION FOLDERS WITHIN THE ATTACHED FOLDER
-- IF IT DOESN'T EXIST, THEN CREATE IT
tell application "Finder"
if not (exists folder site_upload_foldername of this_folder) then
set the site_upload_folder to (make new folder at this_folder with properties {name:site_upload_foldername})
else
set the site_upload_folder to folder site_upload_foldername of this_folder
end if
if not (exists folder archive_foldername of this_folder) then
set the archive_folder to (make new folder at this_folder with properties {name:archive_foldername})
else
set the archive_folder to folder archive_foldername of this_folder
end if
end tell
-- PROCESS EACH OF THE ITEMS ADDED TO THE ATTACHED FOLDER
try
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to the info for this_item
-- CHECK TO SEE IF THE ITEM IS AN IMAGE FILE OF THE ACCEPTED FILE TYPE
if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
tell application "Finder"
--RENAME THE FILE
set date_file_name to my file_name_from_date(this_item)
-- LOOK FOR EXISTING MATCHING ITEMS IN THE DESTINATION FOLDER
-- IF THERE ARE MATCHES, THEN RENAME THE CONFLICTING FILES INCREMENTALLY
-- MOVE THE ITEM TO THE DESTINATION FOLDER
if the label index of this_item is 2 then -- Check if the label is 'red'
my resolve_conflicts(this_item, site_upload_folder)
set the site_file to (duplicate this_item to the site_upload_folder with replacing) as alias
my change_file_name(site_file, site_upload_folder, date_file_name)
set the label index of this_item to 1 -- change the label to orange to show that the web sorting worked
set file_name to the name of site_file
set file_extension to the name extension of site_file
if the file_extension is "" then
set the trimmed_name to the file_name
else
set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
end if
my process_item(site_file, trimmed_name, site_upload_folder)
end if
my resolve_conflicts(this_item, archive_folder)
set the archive_file to (move this_item to the archive_folder with replacing) as alias
my change_file_name(archive_file, archive_folder, date_file_name)
end tell
end if
end repeat
delay 10
my upload_folder(site_upload_folder)
on error error_message number error_number
if the error_number is not -128 then
tell application "Finder"
activate
display dialog "script had an error:" & error_message buttons {"Cancel"} default button 1 giving up after dialog_timeout
end tell
end if
end try
end adding folder items to
-- this sub-routine processes files
on process_item(this_item, trimmed_name, this_folder)
-- NOTE that the variable this_item is a file reference in alias format
-- create thumbnail & recompress image to a good quality setting & resize image if it is too big
try
-- the target path is the destination folder and the new file name
set the file_name to (the trimmed_name & ".jpg") as string
set the thumb_name to (the trimmed_name & "-thumb.jpg") as string
set the file_path to ((this_folder as string) & file_name) as string
set the thumb_path to ((this_folder as string) & thumb_name) as string
with timeout of 900 seconds
tell application "Image Events"
launch -- always use with Folder Actions
set this_image to open file (this_item as string)
scale this_image to size 180
save this_image as JPEG in file thumb_path without icon
close this_image
set this_image to open file (this_item as string)
save this_image as JPEG in file file_path without icon
close this_image
end tell
end timeout
on error error_message
tell application "Finder"
activate
display dialog "process_item had and error:" & error_message buttons {"Cancel"} default button 1 giving up after dialog_timeout
end tell
end try
end process_item
--upload files sub-routine
on upload_folder(site_upload_folder)
set this_folder to (site_upload_folder as alias)
set the these_items to list folder this_folder without invisibles
set upload_count to 0
if not ((number of items in these_items) is 0) then
tell application "Interarchy"
launch
end tell
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the this_path to ((site_upload_folder as string) & this_item) as alias
tell application "Finder"
if the label index of this_path is 0 then -- Check if the label is 'blank'
try
tell application "Interarchy"
store this_path user "username" password "secretword" url "ftp://ftp.server.com/path/" overwrite ofOverwrite
end tell
tell application "Finder"
set the label index of this_path to 1 -- change the label to orange to show that the web sorting worked
my write_log(this_item)
set upload_count to upload_count + 1
end tell
on error error_message number error_number
tell application "Finder"
activate
set the label index of this_path to 2 -- change the label to red to show that the web sorting worked
display dialog "upload_folder had an error:" & error_message buttons {"Cancel"} default button 1 giving up after dialog_timeout
end tell
end try
end if
end tell
end repeat
tell application "Interarchy"
quit
end tell
if upload_count is greater than 0 then
tell application "Finder"
display dialog ((upload_count as string) & " files uploaded.") buttons {"OK"} default button 1 giving up after dialog_timeout
end tell
else
tell application "Finder"
display dialog "No files to upload." buttons {"OK"} default button 1 giving up after dialog_timeout
end tell
end if
end if
end upload_folder
--write stuff to text file
on write_log(this_item)
if not (this_item contains "thumb") then
set text_1 to ""
set text_4 to "-thumb.jpg\" border=\"0\" class=\"thumb\" />"
set the trimmed_name to text 1 thru -5 of the this_item
set the log_file to ((path to desktop) as text) & "Sorddinham Photolog Update.txt"
set text_to_write to ("Entry Body" & return & text_1 & trimmed_name & text_2 & trimmed_name & text_3 & return & "Excerpt" & return & text_1 & trimmed_name & text_2 & trimmed_name & text_4 & return & return)
try
open for access file the log_file with write permission
write text_to_write to file the log_file starting at eof
close access file the log_file
on error
try
close access file the log_file
end try
end try
end if
end write_log
--file name sub-routines
on file_name_from_date(this_item)
--determines a file name in the form of YYYYMMDDHHMMSS
tell application "Finder"
set item_date to creation date of this_item
set item_day to ((day of item_date) as number)
set item_month to ((month of item_date) as number)
set item_year to ((year of item_date) as number)
set item_time to ((time of item_date) as number)
set item_hour to (item_time div hours)
set item_minute to ((item_time - (item_hour * hours)) div minutes)
set item_second to ((item_time - (item_hour * hours)) - item_minute * minutes)
set new_name to (my add_leading_zeros(item_year, 3) ¬
& my add_leading_zeros(item_month, 1) ¬
& my add_leading_zeros(item_day, 1) ¬
& my add_leading_zeros(item_hour, 1) ¬
& my add_leading_zeros(item_minute, 1) ¬
& my add_leading_zeros(item_second, 1))
end tell
return new_name as text
end file_name_from_date
on change_file_name(this_item, target_folder, date_file_name)
tell application "Finder"
set the file_name to the name of this_item
set file_extension to the name extension of this_item
set the name_increment to 0
set the new_name to (the date_file_name & "." & file_extension) as string
repeat
if not (exists document file new_name of the target_folder) then
-- rename to conflicting file
--set the name of document file file_name of the target_folder to the new_name
set the name of this_item to the new_name
exit repeat
else
set the name_increment to the name_increment + 1
end if
set the new_name to (the date_file_name & "_" & (my add_leading_zeros(name_increment, 1) as string) & "." & file_extension) as string
end repeat
end tell
end change_file_name
on resolve_conflicts(this_item, target_folder)
tell application "Finder"
set the file_name to the name of this_item
if (exists document file file_name of target_folder) then
set file_extension to the name extension of this_item
if the file_extension is "" then
set the trimmed_name to the file_name
else
set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
end if
set the name_increment to 1
repeat
set the new_name to (the trimmed_name & "." & (name_increment as string) & "." & file_extension) as string
if not (exists document file new_name of the target_folder) then
-- rename to conflicting file
set the name of document file file_name of the target_folder to the new_name
exit repeat
else
set the name_increment to the name_increment + 1
end if
end repeat
end if
end tell
end resolve_conflicts
-- text sub-routines
on add_leading_zeros(this_number, max_leading_zeros)
-- this sub-routine was found at http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.08.htm
set the threshold_number to (10 ^ max_leading_zeros) as integer
if this_number is less than the threshold_number then
set the leading_zeros to ""
set the digit_count to the length of ((this_number div 1) as string)
set the character_count to (max_leading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & "0") as string
end repeat
return (leading_zeros & (this_number as text)) as string
else
return this_number as text
end if
end add_leading_zeros