MailtoScript/Code

להלן הקוד של הסקריפט. נא לא לשנות את הקוד. מי שרוצה להציע שיפור שיוסיף את השיפורים למטה. אם הם ימצאו בטוחים ושימושים הם ישולבו בסקריפט זה.

[[ltr]]

(*

Mailto - mail files the easy way
Based on Mail scripts from Apple

Nir Soffer 
nirs at freeshell.org

*)


(*

The black list

1. Folders - folders can be magicaly sent using Mail.app and decoded
back to folders including all the subfolders and files at the other
side, by Mail.app. On a PC, folders are flatened to a list of files.
Although cool, folders can make huge attachments, since the user can
send his home folder by mistake. Therefore, treat folder as an error. 
If you want to allow folders, remove them from the black list

2. Volumes - this must be user error.

3. Aliases - They will never work on another Mac.

*)

property theBlackList : {"Folder", "Alias", "Volume"}



(*
open - used when running as droplet
We use run to choose the files, getting the selection. it works 
better with hebrew names this way because we get file reference 
instead of aliases
*)

on open
  run
end open



(*
run - running from the script menu or by double clicking. When double
clicking the application, it should display a help message
*)

on run
  set myPath to (path to current application) as string
  
  tell application "Finder"
    -- get the selection
    set theSelection to the selection
    
    (*
    Running by double clicking? then there is only one item in the
    selection: me. This will not allow to send Mailto script with Mailto
    but the help message is very important
    *)
    if (count of theSelection) is 1 ¬
      and (theSelection as string) = myPath then
      tell me to display dialog ¬
        "You can drop files on this application to mail them using " & ¬
        "Mail.app. Another way to use it is in the Script Menu." buttons ¬
        {"Aha!"} default button 1 with icon note
      return
    end if
    
    -- analize the selection  
    set badSelection to 0
    set theFiles to {}
    
    repeat with theItem in theSelection
      -- check each file aginst the black list
      if theBlackList contains (kind of theItem) then
        set badSelection to badSelection + 1
      else
        -- add to file list
        copy theItem to end of theFiles
      end if
    end repeat
  end tell
  
  -- warn about bad selection
  if badSelection is not 0 then
    if (count of theFiles) is 0 then
      -- this is realy bad, complain and exit
      set theMsg to ¬
        "You selected only Folders, Aliases or Volumes." & return & ¬
        "Next time, try to select only files."
      display dialog theMsg buttons {"I'll try again"} ¬
        default button 1 with icon stop
      return
      
    else if (count of theFiles) is not 0 then
      -- warn about the bad selection but allow to send the files
      set theMsg to "You selected " & ¬
        (count of theFiles) & " files and " & ¬
        badSelection & " Folders, Aliases or Volumes." & return & ¬
        "Next time, try to select only files."
      
      display dialog theMsg ¬
        buttons {"I'll try again", "OK, Mail the Files"} ¬
        default button 2 with icon caution
      
      if button returned of result is "I'll try again" then
        return
      end if
    end if
  end if
  
  -- mail the files
  createMessage for theFiles given subject:(makeSubject for theFiles)
  
end run


(*
createMessage - create message with the files as attachments
Use Mail.app to create the message
Dont chage this unless your know how to script Mail.app
*)

on createMessage for theFiles given subject:theSubject
  tell application "Mail"
    activate
    -- create a new message
    set theMsg to make new compose message at ¬
      beginning of compose messages
    tell theMsg
      set the subject to theSubject as string
      set the content to return & return
      tell content
        repeat with theFile in theFiles
          make new text attachment ¬
            with properties {file name:theFile as alias} ¬
            at before last character of paragraph 2
        end repeat
      end tell
    end tell
    
    -- open the message window to add recipients and text
    make new message editor at the beginning of message editors
    set compose message of message editor 1 to theMsg
  end tell
end createMessage


(*
subjectLine - create a subject line from file names
this routine is not related to Mail.app. 
You can change the subject line as you like.
*)

on makeSubject for theFiles
  
  -- get the name of the first file
  set firstOne to name of (info for (item 1 of theFiles as alias))
  -- convert the name from unicode text to plain ascii
  set firstOne to asciiString(firstOne)
  
  -- create the subject line
  -- <first file name> and others (total <number of files>)
  set total to count of theFiles
  
  if total = 1 then
    set subjectLine to firstOne
    
  else if total = 2 then
    set subjectLine to firstOne & " and one other"
    
  else if total > 2 then
    set subjectLine to firstOne & " and others (total " & total & ")"
    
  end if
  
  return subjectLine
end makeSubject


(*
asciiString - convert to ASCII string

The built in convertion (as string) from Unicode text to string creates
Gibbrish. This will create "?" for any non ascii character like an
Hebrew Unicode character, which is little better.
*)

on asciiString(theText)
  set theResult to {}
  repeat with c in theText
    copy (ASCII character of (ASCII number of c)) to end of theResult
  end repeat
  return theResult as string
end asciiString

[[rtl]]

last edited 2003-02-20 08:33:50 by ניר סופר