Archive of articles classified as' "AppleScript"

Back home

RTF als Email-Formatvorlage

23/03/2011
Sagen wir mal ich habe hier eine RTF-Datei die so aussieht:

Hey REPLACEME1,


how you are doing. Do you have time for REPLACEME2?


Yours REPLACEME3

Dann baut sich dieses Skript hier:

set mytemplate to choose file


set replace_list to {"my friend", "what ever", "hubionmac"}

set replace_markers to {}

repeat with i from 1 to count of replace_list

set replace_markers to replace_markers & {"REPLACEME" & i}

end repeat


tell application "TextEdit"

set mytextfile to open mytemplate

set mytext_parts to attribute run of mytextfile

set mylayout to properties of attribute run of mytextfile

close mytextfile

end tell

repeat with i from 1 to count of replace_list

repeat with k from 1 to count of mytext_parts

set item k of mytext_parts to my replace_chars(item k of mytext_parts as text, item i of replace_markers as text, item i of replace_list as text)

end repeat

end repeat


set mytext to mytext_parts as text


tell application "Mail"

activate

set myMessageText to make new outgoing message with properties {content:mytext, visible:true}

tell myMessageText

set globalcounter to 1

set pointer to 1

repeat with i from 1 to count of mytext_parts

set currentcount to count of every character of item i of mytext_parts

set font of characters globalcounter through (globalcounter + currentcount - 1) to (font of item i of mylayout)

set size of characters globalcounter through (globalcounter + currentcount - 1) to (size of item i of mylayout)

set color of characters globalcounter through (globalcounter + currentcount - 1) to (color of item i of mylayout)

set globalcounter to globalcounter + currentcount

end repeat

(**

--this does not work 100% since set word i does not wait for the command to finish

repeat with i from 1 to count of every word

set myword to word i

if myword is in replace_markers then

set word i to (item ((characters 14 through -1 of (myword as text)) as text as integer) of replace_list) as text

end if

end repeat**)

end tell

end tell


on replace_chars(this_text, search_string, replacement_string)

if this_text contains the search_string then

set od to AppleScript's text item delimiters

set AppleScript's text item delimiters to the search_string

set the item_list to every text item of this_text

set AppleScript's text item delimiters to the replacement_string

set this_text to the item_list as string

set AppleScript's text item delimiters to od

end if

return this_text

end replace_chars

.. aus dieser Vorlage eine neue Email, dessen Text entsprechend formatiert ist. Der Gag dabei ist die Geschwindigkeit und die Tatsache, dass das Ding ohne Zwischenablage und GUI-Skripting die Stil-Angaben der Vorlage übernimmt.
No Comments

RGB Farbcode zu HTML und umgekehrt

16/03/2011

Wenn man in AppleScript eine Farbe angibt (z.B. für einen Text) so geschieht das über eine RGB-Farbwert-Angabe wie {255,5645,4565}. Das Besonder daran, es können eben 16,7 Millionen Farbwerte sein (256*256*256) + jeweils 256 mögliche Werte für den Grad der Transparenz (so glaube ich). Ich fand auch schnell eine Routine zum umwandeln solcher RGB-Werte in HTML-Werte, doch für die andere Richtung schien es nichts zu geben... hier also die Orig-Routine von macosxautomation und meine Routine, die das exakte Gegenteil bewirkt.

set html to RBG_to_HTML({0, 22359, 30583})

return HTML_to_RGB(characters 2 through -1 of html as text)


on RBG_to_HTML(RGB_values)

-- NOTE: this sub-routine expects the RBG values to be from 0 to 65535

set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}

set the the hex_value to ""

repeat with i from 1 to the count of the RGB_values

set this_value to (item i of the RGB_values) div 256

if this_value is 256 then set this_value to 255

set x to item ((this_value div 16) + 1) of the hex_list

set y to item (((this_value / 16 mod 1) * 16) + 1) of the hex_list

set the hex_value to (the hex_value & x & y) as string

end repeat

return ("#" & the hex_value) as string

end RBG_to_HTML


on HTML_to_RGB(HTML_value)

-- NOTE: this sub-routine expects the HTML values to have 6 characters

set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}

set the the RGB_value to {}

set HTML_values to {characters 1 through 2 of HTML_value as text, characters 3 through 4 of HTML_value as text, characters 5 through 6 of HTML_value as text}

repeat with HTML_value in HTML_values

repeat with i from 1 to count of hex_list

if character 1 of HTML_value = item i of hex_list then

set firstvalue to (i - 1) * 16

end if

end repeat

repeat with i from 1 to count of hex_list

if character 2 of HTML_value = item i of hex_list then

set secondvalue to (i - 1) * 1

end if

end repeat

set RGB_value to RGB_value & (firstvalue + secondvalue) * 257

end repeat

return RGB_value

end HTML_to_RGB

No Comments

Word: Kommentare exportieren

15/03/2011
Pages 09' unterstützt deutlich mehr AppleScript-Kommandos als die vorherige Version, dabei wurde aber anscheinend eine Möglichkeit vergessen, auf die Kommentare eines Pages-Dokuments zuzugreifen. Man kann die Kommentare zwar via GUI-Scripting auslesen (was ganz schön aufwendig und fehleranfällig ist) oder sich eines Workarounds (wie so oft in AppleScript) bedienen. Man exportiert das Pages-Dokument als Word-Datei und nutzt das natürlich ebenfalls installierte Microsoft Word:

--15.03.2011 hubionmac.com

--Exports comments to a tab delimited table.


tell application "Microsoft Word"

tell document 1

set myoutput to "Comments of \"" & (name) & "\"" & return & return & "# comment text commented text" & return

repeat with i from 1 to count of every Word comment

--replacing newline characters

set comment_text to content of comment text of Word comment i

set commented_text to content of scope of Word comment i

set myoutput to myoutput & i & "." & tab & my replace_chars(comment_text & tab & commented_text as text, return, "") & return

end repeat

end tell

end tell

set the clipboard to myoutput as text

display dialog "List of comment copied to clipboard (ready for Excel or Numbers)" giving up after 2


--insert into textedit for testing

tell application "TextEdit"

set a to make new document

set text of a to myoutput

end tell



on replace_chars(this_text, search_string, replacement_string)

if this_text contains the search_string then

set AppleScript's text item delimiters to the search_string

set the item_list to every text item of this_text

set AppleScript's text item delimiters to the replacement_string

set this_text to the item_list as string

set AppleScript's text item delimiters to ""

end if

return this_text

end replace_chars

No Comments

iCal: Liste der heutigen Events

11/03/2011
Gibt eine Liste der Events aus, die am heutigen Tag beginnen und auch enden...

getThisDaysEvents_List("Hubi")


on getThisDaysEvents_List(calendarname)

tell application "iCal"

set startdate to (current date) - ((minutes of (current date)) * 60) - ((hours of (current date)) * 60 * 60) - (seconds of (current date))

set enddate to startdate + (3600 * 24)

tell calendar calendarname

set eventnames to summary of every event whose start date > startdate and end date < enddate

set event_starts to start date of every event whose start date > startdate and end date < enddate

end tell

set myoutput to ""

repeat with i from 1 to count of eventnames

set myoutput to myoutput & "" & (characters 1 through -4 of (time string of item i of event_starts)) & "  " & item i of eventnames & return

end repeat

set myoutput to my BubbleSort(every paragraph of myoutput)

set AppleScript's text item delimiters to return

set myoutput to myoutput as text

set AppleScript's text item delimiters to ""

return "Events @ " & date string of (current date) & ":" & return & myoutput

end tell

end getThisDaysEvents_List

on BubbleSort(theList)

if class of theList is list then

set theSize to length of theList

repeat with i from 1 to theSize

repeat with j from 2 to (theSize - i + 1)

if ((item (j - 1) of theList) > (item j of theList)) then

set temp to (item (j - 1) of theList)

set (item (j - 1) of theList) to (item j of theList)

set (item j of theList) to temp

end if

end repeat

end repeat

return theList

else

return false

end if

end BubbleSort

No Comments

Das soll mal AppleScript ganz egal sein…

11/03/2011
Mir ist zwar eine Sinnvolle Anwendung dafür noch nicht untergekommen, aber es funktioniert... damit ist AppleScript alles schei** egal.

tell application "TextEdit"

activate

set a to make new document

set text of a to "blafasel"

beep

ignoring application responses

quit

end ignoring

end tell

No Comments

iTunes: Podcast-Liste als opml exportieren

9/03/2011

Ein weiterer Beweis dafür, dass die Pflege der AppleScript-Funktionen in Apple-Anwendungen keine wirkliche Priorität genießt: Podcasts kann man zwar per Skript aktualisieren, aber deren URL auszulesen ist per Skript nicht möglich... auch ein Export über die Export-Funktion in iTunes ist per Skript ohne weiteres nicht möglich... Die denken sich wahrscheinlich, wenn's wirklich nötig ist, dann halt via GUI-Scripting. So sei es. Dieses Skript exportiert eine Liste der abonnierten Podcasts als *.opml
und wie bei allen GUI-Scripting-Geschichten, funktioniert es nur, wenn der Zugriff für Hilfsgeräte unter Systemeinstellungen/Bedienungshilfe aktiviert ist.

tell application "iTunes"

try

set myplaylist to first item of (every playlist whose special kind is Podcasts)

reveal myplaylist

my clickExportPlaylist()

delay 1

set playlistname to my SaveAsOPML()

do shell script "open -a /Applications/TextEdit.app ~/Desktop/" & playlistname

say "GUI Scripting sucks" using "Agnes"

say "It sucks but it works, Agnes" using "Alex"

say "Bla Bla, Alex" using "Agnes"

on error msg

error msg -- "There is no Podcast Playlist"

end try

end tell


on clickExportPlaylist()

activate application "iTunes"

tell application "System Events"

tell process "iTunes"

-- clicks export Playlist (iTunes 10.1.2)

click menu item 6 of menu 1 of menu item 9 of menu 1 of menu bar item 3 of menu bar 1

end tell

end tell

end clickExportPlaylist


on SaveAsOPML()

activate application "iTunes"

tell application "System Events"

tell process "iTunes"

-- clicks Playlist (iTunes 10.1.2)

click pop up button 1 of group 1 of window 1

delay 1

keystroke "O"

delay 0.25

keystroke return

delay 0.25

--switch to desktop folder

keystroke "d" using command down

--this should be a uniq name for this file

set uniqname to (do shell script "date | md5") & ".opml"

--be sure to overwrite all characters...

keystroke "a" using command down

keystroke uniqname

delay 1

-- hit save

keystroke return

-- wait until save dialog closed

repeat until name of window 1 = "iTunes"

delay 0.5

end repeat

return uniqname

end tell

end tell

end SaveAsOPML

No Comments

Terror Skript aus meinen Anfängen

8/03/2011
Ich habe das mal zum Spaß als App gespeichert, mit dem Photo Booth-Icon versehen und im Dock abgelegt... Ein Klick und schon war alles weg... Bitte also vor dem Ausführen des Skripts ungesicherte Dateien speichern, da sämtliche aktiven Programme im Dock (inkl. Finder) gekillt werden (-9 )!

-- 07.03.2006 hubionmac.com

tell application "System Events"

set myids to {}

set allApps to every application process whose background only is false

repeat with j in allApps

--if name of j is not "Finder" then

set myids to myids & unix id of j

--- end if

end repeat

end tell

set AppleScript's text item delimiters to " "

set myids to myids as text

set AppleScript's text item delimiters to ""

do shell script "kill -9 " & myids

No Comments

Quick&Dirty Port Scanner in AppleScript

7/03/2011
Wenn ich doch nur schon etwas besser in Python programmieren könnte....

--07.03.2011 hubionmac.com

--quick and dirty port scanner... I still have no idea of python :-/

set startport to 80

set endport to 80

set startip to 100

set endip to 100

set network_address to "192.168.0."

set a to {}

repeat with i from startip to endip

set theip to network_address & i

try

do shell script "/sbin/ping -c 2 -t 1 -q " & theip

set thecommand to "import sys

from socket import *

for port in range(int(" & startport & "), int(" & endport & ")+1):

    try:

socket(AF_INET, SOCK_STREAM).connect((\"" & theip & "\", port))

print \"" & theip & "  ----> \",port;

    except: pass;"

set a to a & return & (do shell script "python -c " & quoted form of thecommand)

end try

end repeat


tell application "TextEdit"

make new document

set text of document 1 to a as text

end tell

No Comments

Apple Mail NERV

6/03/2011
Lilly hatte ein Problem (Apple Mail lädt eine Nachricht immer wieder herunter und zeigt keine neuen mehr an) mit ihrem Mac und oh Wunder... ein AppleSkript (von hier) half bei der Lösung ;-P

display dialog "Delete Mail's offline caches?" buttons {"Cancel", "Delete"} default button 2

copy the result as list to {buttonpressed}


if the buttonpressed is "Delete" then

set thePath to (path to library folder from user domain as text) & "Mail"

tell application "Finder"

set theIMAPFolders to (every folder of folder thePath whose name begins with "IMAP") as alias list

end tell

repeat with thisFolder in theIMAPFolders

set theCachePath to (POSIX path of thisFolder & ".OfflineCache")

do shell script "rm -rf " & theCachePath

end repeat

end if

Scheint ein häufiger Bug zu sein, wenn man versucht Emails mit dicken Anhängen über eine langsame Internetverbindungen zu verschicken.
No Comments

Click here after x-minutes

1/03/2011

-- hubionmac.com 01.03.2011

-- remembers mouse position and clicks it after a given time

-- this script uses cliclick by Carsten Blüm (http://www.bluem.net/en/mac/cliclick/)

-- binary should be located in your application folder


say "will copy mouse position in"

say 3

say 2

say 1

say "now"

set mouse_XY to do shell script "afplay /System/Library/Sounds/Tink.aiff; /Applications/cliclick -q"

set AppleScript's text item delimiters to ","

set x to text item 1 of mouse_XY

set y to text item 2 of mouse_XY

set AppleScript's text item delimiters to ""


set waitfor to (text returned of (display dialog "Wieviel Minuten soll ich warten?" default answer 1) as integer) * 60


with timeout of (waitfor + 10) seconds

delay waitfor

do shell script "/Applications/cliclick " & x & " " & y

end timeout

No Comments