Attach File to Email and Set Subject to File Name

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

I have been using this Apple Script to email attachments to pre-defined email addressed. However, the current script sets the file name to include the entire path of the file + file name + extension.

Is there anyway to tweak the script to only include the file name in the subject line?

Code: Select all
tell application "Mail"
   set theAttachment1 to (POSIX path of theFile)
   set theNewMessage to make new outgoing message with properties {subject:theAttachment1, visible:true}
   tell theNewMessage
      make new to recipient at end of to recipients with properties {address:"office@email.com"}
      make new attachment with properties {file name:theAttachment1} at first paragraph
   end tell
end tell
slyfox
 
Posts: 9
Joined: Wed May 25, 2016 10:28 am

This is more of a general AppleScript question but doing a quick google search turned up this which should help: http://forums.macrumors.com/threads/app ... me.927338/
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

With help from this forum - https://discussions.apple.com/message/3 ... 6#30250436 I got a working script:

Code: Select all
tell application "Mail"
   set theAttachment1 to (POSIX path of theFile)
   tell application "Finder" to set theBaseName to the name of (theFile as alias)
   set justBaseName to rich text 1 thru ((offset of "." in theBaseName) - 1) of theBaseName
   set theNewMessage to make new outgoing message with properties {subject:justBaseName, visible:true}
   tell theNewMessage
      make new to recipient at end of to recipients with properties {address:"office@email.com"}
      make new attachment with properties {file name:theAttachment1} at first paragraph
   end tell
end tell
slyfox
 
Posts: 9
Joined: Wed May 25, 2016 10:28 am

Any idea why this script works fine in AppleScript but not with Hazel?

Code: Select all
tell application "Mail"
   set theAttachment1 to (POSIX path of theFile)
   tell application "Finder"
      set justBaseName to do shell script "basename " & theFile's POSIX path & space & "." & name extension of (theFile as alias)
   end tell
   set theNewMessage to make new outgoing message with properties {subject:justBaseName, visible:true}
   tell theNewMessage
      make new to recipient at end of to recipients with properties {address:"office@email.com"}
      make new attachment with properties {file name:theAttachment1} at first paragraph
   end tell
end tell
slyfox
 
Posts: 9
Joined: Wed May 25, 2016 10:28 am

Check the logs and see what error it's producing. Or is it working, just not in the way that you want?
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Error log:

Code: Select all
2016-05-27 07:38:12.427 hazelworker[6024] Smith, John - Psych Report - Clinic Name.docx: Rule Email Psych Report matched.
2016-05-27 07:38:12.601 hazelworker[6024] [Error] AppleScript failed: Error executing AppleScript on file /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx.
2016-05-27 07:38:12.602 hazelworker[6024] OSAScript error: {
    NSLocalizedDescription = "Finder got an error: sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    NSLocalizedFailureReason = "sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,1e01e \"Finder\"]>";
    OSAScriptErrorAppNameKey = Finder;
    OSAScriptErrorBriefMessageKey = "sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorMessageKey = "Finder got an error: sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorNumberKey = 2;
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}
2016-05-27 07:38:14.651 hazelworker[6024] Smith, John - Psych Report - Clinic Name.docx: Rule Email Psych Report matched.
2016-05-27 07:38:14.713 hazelworker[6024] [Error] AppleScript failed: Error executing AppleScript on file /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx.
2016-05-27 07:38:14.713 hazelworker[6024] OSAScript error: {
    NSLocalizedDescription = "Finder got an error: sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    NSLocalizedFailureReason = "sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,1e01e \"Finder\"]>";
    OSAScriptErrorAppNameKey = Finder;
    OSAScriptErrorBriefMessageKey = "sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorMessageKey = "Finder got an error: sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorNumberKey = 2;
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}
2016-05-27 07:38:16.748 hazelworker[6024] Smith, John - Psych Report - Clinic Name.docx: Rule Email Psych Report matched.
2016-05-27 07:38:16.783 hazelworker[6024] [Error] AppleScript failed: Error executing AppleScript on file /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx.
2016-05-27 07:38:16.784 hazelworker[6024] OSAScript error: {
    NSLocalizedDescription = "Finder got an error: sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    NSLocalizedFailureReason = "sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,1e01e \"Finder\"]>";
    OSAScriptErrorAppNameKey = Finder;
    OSAScriptErrorBriefMessageKey = "sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorMessageKey = "Finder got an error: sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorNumberKey = 2;
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}
2016-05-27 07:38:20.824 hazelworker[6024] Smith, John - Psych Report - Clinic Name.docx: Rule Email Psych Report matched.
2016-05-27 07:38:20.854 hazelworker[6024] [Error] AppleScript failed: Error executing AppleScript on file /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx.
2016-05-27 07:38:20.855 hazelworker[6024] OSAScript error: {
    NSLocalizedDescription = "Finder got an error: sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    NSLocalizedFailureReason = "sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,1e01e \"Finder\"]>";
    OSAScriptErrorAppNameKey = Finder;
    OSAScriptErrorBriefMessageKey = "sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorMessageKey = "Finder got an error: sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `basename /Users/slyfox/Dropbox/TEST/TEST Files (2016)/Smith, John - Psych Report - Clinic Name.docx .docx'";
    OSAScriptErrorNumberKey = 2;
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}


Comment from https://discussions.apple.com/message/3 ... 6#30258937 but I don't know what it means

OS X is a UNIX system, and if you pass unquoted filenames that contain white space in the shell, it will abort. If in an AppleScript do shell script, you must use variable's quoted form to surround the whitespace with quotes, which satisfies the shell.
slyfox
 
Posts: 9
Joined: Wed May 25, 2016 10:28 am

Basically, you need to put quotes around the file name just in case there are spaces in it. That said, I'm not sure it's necessary to resort to a shell script to grab the filename. I think the link I posted earlier had an AppleScript way of doing it.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

This script works based on - https://discussions.apple.com/message/30259112#30259112

Code: Select all
set f to (theFile as alias)
tell application "System Events"
    set {n, e} to f's {name, name extension}
    if e = "" then
        set m to f's name
    else
        set m to n's text 1 thru -((count e) + 2)
    end if
end tell
--return {f, n, m} -- for test


tell application "Mail"
    tell (make new outgoing message with properties {subject:m, visible:true})
        make new to recipient at end of to recipients with properties {address:"office@email.com"}
        make new attachment with properties {file name:f's POSIX path} at paragraph 1
    end tell
end tell
slyfox
 
Posts: 9
Joined: Wed May 25, 2016 10:28 am


Return to Support