Add attached filenames to subject in the applescript

applescriptfinder

i'm a newbie with applescript. I managed to get together a little script that add selected files from finder to a email attachment as a service workflow. Now i stuck on the last issue.
I'd like to add the filename to the subject.
Here 's the applescript. Could anyone help?

property mailSubjectPrefix : "Datei(en) für dich: "
property subjAttNameSeparator : ", "
property attachSeparator : "*******"
property attachPrefix : "Anhänge:"

set theSubject to "Datei(en): "
set theContent to "Datei(en) für dich:"
set recipientAddress to {}


tell application "Contacts"

    set recipientAddress to "mail@mail.com"

end tell




tell application "Finder"
-- Make a list to gather the names of the selected files
set fileAliases to {}
-- Get the selection of the frontmost Finder window
set fileSelection to the selection
-- Iterate of the selection
repeat with fileItem in fileSelection
    copy the fileItem as alias to the end of fileAliases
end repeat
-- Check if the selection is not empty
if the number of items of fileAliases is 0 then
    -- Audible feedback, so the script always does something.
    beep
else
    -- Now talk to mail to create the message
    tell application "Mail"
        set newMessage to make new outgoing message at beginning with properties { content:theContent, visible:true}
        set mailSubject to {}
        -- Set a recipient



        tell newMessage
            make new to recipient at end with properties {address:recipientAddress}



        set MailSubject to fileAliases

        set oTID to AppleScript's text item delimiters
        set AppleScript's text item delimiters to subjAttNameSeparator
        set subject to mailSubjectPrefix & (mailSubject as string)
        set AppleScript's text item delimiters to oTID




        end tell
        -- Attach all the selected files
        repeat with fileAlias in fileAliases
            make new attachment with properties {file name:fileAlias} at after the last paragraph of newMessage

        end repeat




        -- Put Mail in the foreground
        activate


    end tell
end if
end tell

Best Answer

Simply change this line:

    set MailSubject to fileAliases

to this:

    set mailSubject to fileAliases as string

    tell application "Finder" to get insertion location
    set text item delimiters of AppleScript to result

    set mailSubject to rest of text items of mailSubject

ADDENDUM:

I hope you don't my observation that the script you present in your question contains a lot of redundant and/or badly formed code. It works just fine, so it's not of great consequence beyond one of aesthetics. However, a neater, well-formed script makes it easier to identify errors and—in the case of longer scripts that perform more tasks—will run more efficiently/faster.

So, with this in mind, I took the liberty to re-write your script, which I hope might be useful as a learning tool, or simply just be easier to get along with if you want to make further changes to it yourself.

    property mailSubjectPrefix : "Datei(en) für dich: "
    property subjAttNameSeparator : ", "
    property attachSeparator : "*******"
    property attachPrefix : "Anhänge:"

    set theSubject to "Datei(en): "
    set theContent to "Datei(en) für dich:\n\n"
    set recipientAddress to "mail@mail.com"


    -- Get the selection of the frontmost Finder window
    -- as a list of aliases
    tell application "Finder"
        set fileAliases to the selection as alias list
        set text item delimiters of AppleScript to insertion location
        set fileNames to rest of text items of (fileAliases as text)
    end tell


    -- Check if the selection is not empty
    if the number of fileAliases is 0 then return beep

    -- Now talk to mail to create the message
    tell application "Mail"
        set text item delimiters of AppleScript to subjAttNameSeparator
        tell (make new outgoing message at beginning ¬
            with properties {content:theContent, visible:true, subject:mailSubjectPrefix & (fileNames as string)})
    
            -- Add the recipient
            make new to recipient at end with properties {address:recipientAddress}
    
            -- Attach all the selected files
            repeat with fileAlias in fileAliases
                make new attachment with properties {file name:fileAlias} at after the last paragraph
            end repeat
    
        end tell

        -- Put Mail in the foreground
        activate
    end tell

If you have any queries to need clarification on any points, feel free to leave a comment and I'll get back to you.