• Apfeltalk ändert einen Teil seiner Allgemeinen Geschäftsbedingungen (AGB), das Löschen von Useraccounts betreffend.
    Näheres könnt Ihr hier nachlesen: AGB-Änderung
  • Der Frühling ist auch in den eingesandten Fotos deutlich zu erkennen. Zeigt uns, wer Euer Favorit ist! hier geht es lang für Euer Voting --> Klick

AppleScript mit Parametern aus einem Shell Script füttern

ascotmac

Erdapfel
Registriert
02.07.12
Beiträge
5
Hallo ,

ich habe ein Shell Script welches mir eine Logdatei von einem UNIX Server holt und dies in eine PDF Datei umwandelt. Bis hierher funktioniert noch alles.

Ich sollte gleich sagen, dass ich AppleScript Neuling bin und nicht weiss was ich da tue. Ich hoffe aber mit solchen kleinen Anforderung mich schnell in die Materie einzuarbeiten ;O)

Nun möchte ich diese PDF Datei per E-Mail über AppleScript verschicken. Ich habe mich hier im Forum schon umgesehen und einen Beitrag gefunden der ein ähnliches Thema behandelt. Daraufhin habe ich das AppleScript von Doug Adams ein wenig an meine Bedürfnisse angepasst.

Es wird auch eine E-Mail erstellt. Der Text für den Anhang wird angezeigt. Die Betreffzeile ist in Ordnung. Leider wird aber weder der Empfänger, noch der Absender noch die PDF Datei als Anhang hinzugefügt. :-c

Folgender Aufruf startet das AppleScript:

Code:
[email protected]
Betr=Konfiguration
[email protected]
FileName=<PfadtoKonfDatei>/20120629_TS-log.pdf
Zusatz="Radkappe"
osascript conf_pdf2e-mail.scpt "${Empf}" "${Betr}" "${Abs}" "${FileName}" "${Zusatz}"

Hier das angepasste AppleScript:
Code:
[B]property[/B] [COLOR=#498f2a]mailSubjectPrefix[/COLOR] : "Konfiguration: "
[B]property[/B] [COLOR=#498f2a]attachSeparator[/COLOR] : "*******"
[B]property[/B] [COLOR=#498f2a]attachPrefix[/COLOR] : "Konfiguration:"


[B]on[/B] [COLOR=#053df5][B]run[/B][/COLOR] [COLOR=#498f2a]argv[/COLOR]
[COLOR=#5e6161][COLOR=#000000]    [/COLOR](*[/COLOR]
[COLOR=#5e6161]    "Selected Tracks As Mail Attachment" for iTunes[/COLOR]
[COLOR=#5e6161]    written by Doug Adams[/COLOR]
[COLOR=#5e6161]    [email protected][/COLOR]
[COLOR=#5e6161]
[/COLOR]
[COLOR=#5e6161]    v1.0 july 29 2006[/COLOR]
[COLOR=#5e6161]    -- initial release[/COLOR]
[COLOR=#5e6161]
[/COLOR]
[COLOR=#5e6161]    Get more free AppleScripts and info on writing your own[/COLOR]
[COLOR=#5e6161]    at Doug's AppleScripts for iTunes[/COLOR]
[COLOR=#5e6161]    http://www.dougscripts.com/itunes/[/COLOR]
[COLOR=#5e6161]    *)[/COLOR]
    
    [B]tell[/B] [COLOR=#053df5][I]application[/I][/COLOR] "Mail"
        
        [B]set[/B] [COLOR=#498f2a]mailSubject[/COLOR] [B]to[/B] [COLOR=#053df5][I]item[/I][/COLOR] 2 [B]of[/B] [COLOR=#498f2a]argv[/COLOR]
        [B]set[/B] [COLOR=#053df5][B]mailto[/B][/COLOR] [B]to[/B] [COLOR=#053df5][I]item[/I][/COLOR] 1 [B]of[/B] [COLOR=#498f2a]argv[/COLOR]
        [B]set[/B] [COLOR=#498f2a]mailfrom[/COLOR] [B]to[/B] [COLOR=#053df5][I]item[/I][/COLOR] 3 [B]of[/B] [COLOR=#498f2a]argv[/COLOR]
        
[COLOR=#053df5][COLOR=#000000]        [B]set[/B] [/COLOR][COLOR=#498f2a]new_mess[/COLOR][COLOR=#000000] [B]to[/B] [/COLOR][B]make[/B]new[I]outgoing message[/I][/COLOR]
[COLOR=#498f2a][COLOR=#000000]        [B]tell[/B] [/COLOR]new_mess[/COLOR]
            [B]set[/B] [COLOR=#8436d3]visible[/COLOR] [B]to[/B] [COLOR=#5f348c]true[/COLOR]
[COLOR=#5e6161][COLOR=#000000]            [/COLOR]--set content to (fnoms & return & content)[/COLOR]
[COLOR=#5e6161][COLOR=#000000]            [/COLOR]--set content to (return & attachSeparator & return & attachPrefix & return & return)[/COLOR]
[COLOR=#8436d3][COLOR=#000000]            [B]set[/B] [/COLOR]content[COLOR=#000000] [B]to[/B] ([/COLOR]return[COLOR=#000000] & [/COLOR][COLOR=#498f2a]attachPrefix[/COLOR][COLOR=#000000] & [/COLOR]return[COLOR=#000000] & [/COLOR]return[COLOR=#000000])[/COLOR][/COLOR]
[COLOR=#5e6161][COLOR=#000000]            [/COLOR]--repeat with this_atch in atch[/COLOR]
            [B]tell[/B] [COLOR=#8436d3]content[/COLOR]
[COLOR=#5e6161][COLOR=#000000]                [/COLOR]-- make new attachment with properties {file name:item 4 of argv} at after last paragraph -- at after the last word of the second paragraph[/COLOR]
[COLOR=#053df5][COLOR=#000000]                [/COLOR][B]make [/B]new [I]attachment [/I]with properties[COLOR=#000000] {[/COLOR][COLOR=#8436d3]file name[/COLOR][COLOR=#000000]:[/COLOR][I]item[/I][COLOR=#000000] 4 [B]of[/B] [/COLOR][COLOR=#498f2a]argv[/COLOR][COLOR=#000000]}[/COLOR][/COLOR]
            [B]end[/B] [B]tell[/B]
[COLOR=#5e6161][COLOR=#000000]            [/COLOR]--     tell application "Finder" to set mailSubject to mailSubject & name of item 4 of argv & space                [/COLOR]
[COLOR=#5e6161][COLOR=#000000]            [/COLOR]--end repeat[/COLOR]
            
            [B]set[/B] [COLOR=#8436d3]subject[/COLOR] [B]to[/B] [COLOR=#498f2a]mailSubjectPrefix[/COLOR] & [COLOR=#053df5][I]item[/I][/COLOR] 2 [B]of[/B] [COLOR=#498f2a]argv[/COLOR] & " " & [COLOR=#053df5][I]item[/I][/COLOR] 5 [B]of[/B] [COLOR=#498f2a]argv[/COLOR]
        [B]end[/B] [B]tell[/B]
[COLOR=#053df5][COLOR=#000000]        [/COLOR][B]activate[/B][/COLOR]
    [B]end[/B] [B]tell[/B]
[B]end[/B] [COLOR=#053df5][B]run[/B][/COLOR]

Fein wäre auch, wenn ich angeben könnte welche Signatur verwendet werden sollte und dass die E-Mail als Text und nicht als HTML E-Mail versendet wird. Kann mir jemand auf die Sprünge helfen?

Besten Dank schon mal...

ascotmac
 

fyysh

Schweizer Glockenapfel
Registriert
25.01.10
Beiträge
1.386
Mir stellt sich die Frage, warum du überhaupt den Umweg über deinen Mac gehst und nicht gleich vom Unix Server die Mail verschickst.
 

ascotmac

Erdapfel
Registriert
02.07.12
Beiträge
5
Zum einen um zu lernen und zum anderen werden die Logs auf meinem Mac abgelegt und sollen auch von dort weiter verarbeitet werden.
vom UNIX Server wäre zu einfach *gg*
 

ascotmac

Erdapfel
Registriert
02.07.12
Beiträge
5
bin jetzt schon einen Schritt weiter, oder zwei?

Der Empfänger wird übernommen, der Anhang hinzugefügt.

Bleibt nur noch der Absender mit der richtigen Signatur und das Format der E-Mail von HTML -> PlainText.
Hier der Code:

Code:
property mailSubjectPrefix : "Konfiguration: "
property attachSeparator : "*******"
property attachPrefix : "Konfiguration:"

on run argv
    (*
    "Selected Tracks As Mail Attachment" for iTunes
    written by Doug Adams
    [email protected]

    v1.0 july 29 2006
    -- initial release

    Get more free AppleScripts and info on writing your own
    at Doug's AppleScripts for iTunes
    http://www.dougscripts.com/itunes/
    *)
    
    tell application "Mail"
        
        --        set mailto to item 1 of argv
        set mailSubject to item 2 of argv
        set mailfrom to item 3 of argv
        
        set new_mess to make new outgoing message
        tell new_mess
            set visible to true
            --set content to (fnoms & return & content)
            --set content to (return & attachSeparator & return & attachPrefix & return & return)
            set content to (return & attachPrefix & return & return)
            --repeat with this_atch in atch
            tell content
                -- make new attachment with properties {file name:item 4 of argv} at after last paragraph -- at after the last word of the second paragraph
                make new attachment with properties {file name:item 4 of argv} at after last paragraph
            end tell
            tell application "Finder" to set mailSubject to mailSubject & space
            --end repeat
            make new to recipient at end of to recipients with properties {name:"Hans Mustermann", address:item 1 of argv}
            set subject to mailSubjectPrefix & item 2 of argv & " " & item 5 of argv
        end tell
        activate
    end tell
end run
 

sedna

Schweizer Glockenapfel
Registriert
22.10.08
Beiträge
1.375
Hallo,
ohne auf Grundsätzliches eingehen zu wollen ... hier quasi dein Skript (ohne den ganzen "deaktivierten" Kram)
Code:
property mailSubjectPrefix : "Konfiguration: "
--property attachSeparator : "*******"  -- ???
property attachPrefix : "Konfiguration:"




on run argv
    
tell application "Mail"
[COLOR=#5F6161]--set default message format to plain format[/COLOR]        

set new_mess to make new outgoing message
tell new_mess
set visible to true
make new to recipient at beginning of to recipients with properties {address:item 1 of argv}
set sender to item 3 of argv
set subject to mailSubjectPrefix & item 2 of argv & " " & item 5 of argv
set content to (return & attachPrefix & return & return)
            
make new attachment with properties {file name:item 4 of argv}
set its message signature to signature 1 of application "Mail" --Nummer oder "Name" der Signatur 
end tell
        
activate
end tell
end run

Ich glaube message signature spinnt in Lion und HTML Emails verschicke ich nicht. Der Befehl set default message format to plain format setzt halt das Email Format für alle ausgehende Emails auf Reinen Text...

Gruß
 
Zuletzt bearbeitet:

fyysh

Schweizer Glockenapfel
Registriert
25.01.10
Beiträge
1.386
Zum einen um zu lernen und zum anderen werden die Logs auf meinem Mac abgelegt und sollen auch von dort weiter verarbeitet werden.
vom UNIX Server wäre zu einfach *gg*

Lass mich dennoch folgendes bemerken:
Geschickter wäre es, vom Unix Server die Mail zu verschicken und zwar zum Empfänger, den du mit dem Applescript erreichen willst und zum Mac, wenn du's denn da unbedingt archivieren musst und, falls die Postfach-Archivierung nicht reicht, eine Regel für diese Mails zu erstellen und damit ein Script zu triggern, der dir die PDF macht (falls nicht schon auf dem Server geschehen) und irgendwo im FS ablegt.

Hätte v.a. den Vorteil, dass die Mail, die du per AS verschicken willst (=> Thread), auch ankommt, wenn dein Mac nicht läuft.


Aber wenn's nur zur Übung ist... auch egal. ;)
 

ascotmac

Erdapfel
Registriert
02.07.12
Beiträge
5
Hallo,

vielen Dank für die Unterstützung und Anregungen.

@sedna
ich verschicke auch keine E-Mails im HTML Format, bei mir ist der Standard: Plain/Text.
Wenn ich aber in die mit AS erzeugte E-Mail eine Signatur erzeuge, dann ist die Signatur formatiert. Von da her kam ich darauf die E-Mail explizit umzustellen. Ich arbeite aber noch mit 10.6. Die Tipps mit der Signatur und dem Plain Text haben leider nicht funktioniert, aber da werde ich beizeiten mal beigehen. Stand jetzt ist, dass das AS mir eine E-Mail erstellt mit Empfänger, Absender, Subject, Mail-Text und gewünschtem Anhang. Prima bis hierhin.

Falls es jemanden interessiert, hier das AS.

Code:
[B]property[/B] [COLOR=#498F2A]mailSubjectPrefix[/COLOR] : "Konfiguration: "
[B]property[/B] [COLOR=#498F2A]attachPrefix[/COLOR] : "Konfiguration:"


[B]on[/B] [COLOR=#053DF5][B]run[/B][/COLOR] [COLOR=#498F2A]argv[/COLOR]
[COLOR=#5E6161][COLOR=#000000]	[/COLOR](*[/COLOR]
[COLOR=#5E6161]	"Selected Tracks As Mail Attachment" for iTunes[/COLOR]
[COLOR=#5E6161]	written by Doug Adams[/COLOR]
[COLOR=#5E6161]	[email protected][/COLOR]
[COLOR=#5E6161]
[/COLOR]
[COLOR=#5E6161]	v1.0 july 29 2006[/COLOR]
[COLOR=#5E6161]	-- initial release[/COLOR]
[COLOR=#5E6161]
[/COLOR]
[COLOR=#5E6161]	Get more free AppleScripts and info on writing your own[/COLOR]
[COLOR=#5E6161]	at Doug's AppleScripts for iTunes[/COLOR]
[COLOR=#5E6161]	http://www.dougscripts.com/itunes/[/COLOR]
[COLOR=#5E6161]	*)[/COLOR]
	
[COLOR=#5E6161][COLOR=#000000]	[/COLOR](* 20120703 - Anpassungen/Änderungen *)[/COLOR]
	[B]tell[/B] [COLOR=#053DF5][I]application[/I][/COLOR] "Mail"
[COLOR=#5E6161][COLOR=#000000]		[/COLOR](* Betreff auf 2tes Argument setzen *)[/COLOR]
		[B]set[/B] [COLOR=#498F2A]mailSubject[/COLOR] [B]to[/B] [COLOR=#053DF5][I]item[/I][/COLOR] 2 [B]of[/B] [COLOR=#498F2A]argv[/COLOR]
[COLOR=#5E6161][COLOR=#000000]		[/COLOR](* Neue E-Mail erstellen mit folgenden Eigenschaften: Absender, Betreff, Sichtbar? *)[/COLOR]
		[B]set[/B] [COLOR=#498F2A]new_message[/COLOR] [B]to[/B] [COLOR=#053DF5][B]make[/B][/COLOR] [COLOR=#053DF5]new[/COLOR] [COLOR=#053DF5][I]outgoing message[/I][/COLOR] [COLOR=#053DF5]with properties[/COLOR] {[COLOR=#8436D3]sender[/COLOR]:[COLOR=#053DF5][I]item[/I][/COLOR] 3 [B]of[/B] [COLOR=#498F2A]argv[/COLOR], [COLOR=#8436D3]subject[/COLOR]:[COLOR=#498F2A]mailSubjectPrefix[/COLOR] & [COLOR=#053DF5][I]item[/I][/COLOR] 2 [B]of[/B] [COLOR=#498F2A]argv[/COLOR] & " " & [COLOR=#053DF5][I]item[/I][/COLOR] 5 [B]of[/B] [COLOR=#498F2A]argv[/COLOR], [COLOR=#8436D3]visible[/COLOR]:[COLOR=#5F348C]true[/COLOR]}
		
[COLOR=#498F2A][COLOR=#000000]		[B]tell[/B] [/COLOR]new_message[/COLOR]
[COLOR=#5E6161][COLOR=#000000]			[/COLOR](* E-Mail Text setzen: Leerzeile Text Leerzeile Leerzeile *)[/COLOR]
[COLOR=#8436D3][COLOR=#000000]			[B]set[/B] [/COLOR]content[COLOR=#000000] [B]to[/B] ([/COLOR]return[COLOR=#000000] & [/COLOR][COLOR=#498F2A]attachPrefix[/COLOR][COLOR=#000000] & [/COLOR]return[COLOR=#000000] & [/COLOR]return[COLOR=#000000])[/COLOR][/COLOR]
			[B]tell[/B] [COLOR=#8436D3]content[/COLOR]
[COLOR=#5E6161][COLOR=#000000]				[/COLOR](*Anhang erstellen mit den Eigenschaften: Dateiname mit kompletten Pfad *)[/COLOR]
[COLOR=#053DF5][COLOR=#000000]				[/COLOR][B]make[/B][COLOR=#000000] [/COLOR]new[COLOR=#000000] [/COLOR][I]attachment[/I][COLOR=#000000] [/COLOR]with properties[COLOR=#000000] {[/COLOR][COLOR=#8436D3]file name[/COLOR][COLOR=#000000]:[/COLOR][I]item[/I][COLOR=#000000] 4 [B]of[/B] [/COLOR][COLOR=#498F2A]argv[/COLOR][COLOR=#000000]} [/COLOR]at[COLOR=#000000] [B]after[/B] [B]last[/B] [/COLOR][I]paragraph[/I][/COLOR]
			[B]end[/B] [B]tell[/B]
[COLOR=#5E6161][COLOR=#000000]			[/COLOR](* Empfänger eintragen: SIchtbarer Name, E-Mail Adresse *)[/COLOR]
[COLOR=#053DF5][COLOR=#000000]			[/COLOR][B]make[/B][COLOR=#000000] [/COLOR]new[COLOR=#000000] [/COLOR][I]to recipient[/I][COLOR=#000000] [/COLOR]at[COLOR=#000000] [B]end[/B] [B]of[/B] [/COLOR][I]to recipients[/I][COLOR=#000000] [/COLOR]with properties[COLOR=#000000] {[/COLOR][COLOR=#8436D3]name[/COLOR][COLOR=#000000]:"Hans Mustermann", [/COLOR][COLOR=#8436D3]address[/COLOR][COLOR=#000000]:[/COLOR][I]item[/I][COLOR=#000000] 1 [B]of[/B] [/COLOR][COLOR=#498F2A]argv[/COLOR][COLOR=#000000]}[/COLOR][/COLOR]
		[B]end[/B] [B]tell[/B]
[COLOR=#053DF5][COLOR=#000000]		[/COLOR][B]activate[/B][/COLOR]
		
	[B]end[/B] [B]tell[/B]
[B]end[/B] [COLOR=#053DF5][B]run[/B][/COLOR]

Ich werde mir mal die Doku zu AppleScript ansehen, scheint eine "lustige" Sache zu sein. Macht jedenfalls Spaß.
Vielen Dank noch mal.

ascotmac
 

ascotmac

Erdapfel
Registriert
02.07.12
Beiträge
5
@fyysh
Lass mich dennoch folgendes bemerken:
Geschickter wäre es, vom Unix Server die Mail zu verschicken und zwar zum Empfänger, den du mit dem Applescript erreichen willst und zum Mac, wenn du's denn da unbedingt archivieren musst und, falls die Postfach-Archivierung nicht reicht, eine Regel für diese Mails zu erstellen und damit ein Script zu triggern, der dir die PDF macht (falls nicht schon auf dem Server geschehen) und irgendwo im FS ablegt.

Hätte v.a. den Vorteil, dass die Mail, die du per AS verschicken willst (=> Thread), auch ankommt, wenn dein Mac nicht läuft.

Vielen Dank für Deine Hilfestellung. Der UNIX Server ist nur temporär bei mir. Daher kann und soll er die E-Mail nicht verschicken. Ich ziehe mir bestimmte Teile der Hardware Konfiguration zu Dokumentationszwecken. Von daher läuft das AS auch nur, wenn ich einen solchen Server fertig konfiguriert habe. Es ging mir hier um einen abgeschlossen "Workflow" der automatisiert werden sollte, da ich ein fauler Mensch bin. Bisher musste ich die E-Mail nach den Arbeiten immer manuell erstellen und verschicken, damit weitere Stellen mit den Daten arbeiten können. Nun ist es so, dass ein lokales Script das eigentliche Script welches die Daten erfasst auf den UNIX Server überträgt, eine Logdatei erstellt, diese auf meinen Rechner holt, in eine PDF Datei umwandelt, dieses PDF mit einem Kurztext in ein Wiki schreibt und nun zum krönenden Abschluss mir die E-Mail erstellt die ich dann nach kurzer Prüfung verschicke. So kann ich nichts vergessen und die Daten landen da wo sie hin sollen. ;O)
Ich weiss, einfacher geht immer. Aber wie schon gesagt, es ging mir auch einfach darum die Möglichkeiten auszuloten und ein wenig mit AS zu üben.

Das nächste "Projekt" steht dann auch schon an. Da geht es darum bestimmte E-Mail nach dem Eintreffen über ein AS so zu filtern, dass Schlussendlich bestimmte Informationen in dein Excel Liste geschrieben werden. Hier geht es dann um einen langfristige Auswertung von Rückmeldungen. Aber das ist nun was anderes. Auf jeden Fall werde ich hier weiters stöbern ...

ascotmac