Heikos Little Helpers

From Wurst-Wasser.net
Revision as of 09:02, 23 August 2010 by Heiko (talk | contribs) (→‎Download)
Jump to navigation Jump to search

About

Heiko's Little Helpers are a bunch of AppleScripts I wrote to do frequently needed jobs for me...
Originally I programmed an application named Sake to do that, but it became more complex than I anticipated...that's why I decided to discontinue Sake and switch to FastScripts.

What?

There are some things I often to by hand - this really bothered me. Therefore I sought a way to do this more efficiently.

System Requirements

Setup

  • Install FastScripts
  • Download this scripts (link below), unpack and move them either into...
    • ~/Library/Scripts (usable for your own user only)
    • /Library/Scripts (usable for all users of your Mac)

Usage

  • Example: Archive Selected File/Folder.scpt
    • Select the files you want to archive, in this case "Miscellaneous Scripts", and select Archive this! in FastScripts menu...
      Heikoslittlehelpers20091222a1.png
    • ...and you get this:
      Heikoslittlehelpers20091222b1.png

What it can do for you!

Script Function
Paste Timestamp.scpt Pastes a timestamp like "2009-12-22 16:29" into the current text editor.
Paste Log Timestamp.scpt Pastes timestamp like this: "2009-12-22, 16:32, hk: ". This is my favourite. I don't remember what it was like before I used this!
Archive Selected File/Folder.scpt You'll gonna love this: Creates an archive from your selected file/folder and adds an timestamp to the filename. That's sooo handy for my source backups.
Leo Selected Text.scpt Looks up selected text in http://www.leo.org .
Look Up Selected Text In Dictionary.scpt Looks up selected text in MacOS X's Dictionary.
Google Selected Text.scpt Looks up selected text in Google
Google Selected Text For Images.scpt Looks up selected text in Google Image search
Dial Selected Number.scpt Dials selected number with iLink Direct
Decrease Volume.scpt Decreases system volume
Increase Volume.scpt Decreases system volume
Mute Volume.scpt Mutes system volume
Toggle Mute.scpt Toggles muting of system volume
iTunes Rating Scripts.scpt Rates selected or current tracks
Google Album Artwork Of Current Track.scpt Guess what.
Google Album Artwork Of Selected Track.scpt Guess what.
Google Lyrics Of Current Track.scpt Guess what.
Google Lyrics Of Selected Track.scpt Guess what.
Paste iTunes Current Track.scpt Pastes the current iTunes track like this: "The Beez - Die Warteschleife - Die Warteschleife (Radio Edit)".
Create Checksum File.scpt Creates checksum files for the selected file(s).
Compare Checksum File.scpt Checks whether the checksum files exist and match, or not.

Download

Media:HeikosLittleHelpers20100823.zip

Sources

You might be interested in how that stuff works. Take a look:


HeiMac:Little Helpers Scripts heiko$ ./getSourceOfAppleScripts.sh 
----------------------------------------------------------------------
Dumping ./Archive Selected File or Folder.scpt
(* Script to archive selected files/folders and name them 

History:

2009-11-12, 12.10, hk: First version is working.

ToDo:

-strip leading directories from archive, so that they don't get created when expanding...



*)
on run (argv)
tell application "Finder"
set selectedItems to selection
if ((count of selectedItems) is 1) then
# else if ((count of selectedItems) is greater than 1) then
set theItem to first item in selectedItems #as alias
#get class of theItem
set theItemClass to class of theItem
log theItemClass
set theItemName to name of theItem
set theItemContainer to (container of theItem) as alias
set itemPath to POSIX path of (theItem as alias)
set itemContainerPath to POSIX path of theItemContainer
if (theItemClass is folder) then (* not using type because it's localized, class is not *)
(* remove trailing "/" *)
set l to length of itemPath
set itemPath to text 1 thru (l - 1) of itemPath
log "shortened: " & itemPath
else
# mach nix
end if
set zipFileName to theItemName & " " & my timestamp() & ".zip"
#set zipFilePath to itemContainerPath & "" & theItemName & "_" & my timestamp() & ".zip"
#trasht alle dirs im zip set cmd to "zip -j -r \"" & zipFilePath & "\" \"" & itemPath & "\"" # -m
set cmd to "cd \"" & itemContainerPath & "\" && zip -r \"" & zipFileName & "\" \"" & theItemName & "\"" # -m
# set cmd to "zip \"" & zipFilePath & "\" -x .\\* \"" & itemPath & "\""
log cmd
do shell script cmd
else
display dialog "Please select one files/folders in Finder." buttons "Allright, I do whatever you say!"
end if
end tell
end run









(* get a time stamp *)
on timestamp()
set dateNow to current date
set dateYear to year of dateNow
set dateMonth to addLeadingZeros(2, (month of dateNow as number) as string)
set dateDay to addLeadingZeros(2, day of dateNow)
set dateHour to addLeadingZeros(2, hours of dateNow)
set dateMinute to addLeadingZeros(2, minutes of dateNow)
set dateString to (dateYear & "-" & dateMonth & "-" & dateDay & " " & dateHour & "-" & dateMinute) as string
end timestamp

(* Liefert von einer Zahl n leading zeros zurück *)
on addLeadingZeros(zeroesCount, value)

(* nimmt die letzten zeroesCount Stellen des Strings *)
set newNumber to (text -zeroesCount thru -1 of ("0000000000000000" & value)) (* Da musch drauf kommen: http://macscripter.net/viewtopic.php?id=24533 *)
log "newNumber: " & newNumber

return (newNumber)
end addLeadingZeros


---------------------------------------------------------------------- Dumping ./Compare Checksum File.scpt (* Script to check an md5 checksum

ToDo:

- escape quotes in filenames, because that will result in an error
- do the fileCount over all, not only every one selected folder (multiple dialogs)
- optional crc using "cksum" instead of md5 (cksum only on Snow Leopard (10.6) and maybe higher)

*)

(* Global Vars *)
global fileCount, logMessage, dialogTooMuchFilesMessage01, dialogTooMuchFilesMessage02, dialogTooMuchFilesYes, dialogTooMuchFilesNo, dialogNothingSelectedMessage, dialogNothingSelectedButton, dialogErrorOccurredMessage, dialogErrorOccurredButton, dialogChecksumFileMissingMessage01, dialogChecksumFileMissingMessage02, dialogChecksumFileMissingButton, dialogErrorWord, dialogAttentionWord, dialogChecksumMismatchMessage01, dialogChecksumMismatchMessage02


(* Properties (English) *)
property dialogAttentionWordEnglish : "ATTENTION! "
property dialogErrorWordEnglish : "Error: "
property dialogTooMuchFilesMessage01English : "Are you sure you want to check checksums for "
property dialogTooMuchFilesMessage02English : " files?"
property dialogTooMuchFilesYesEnglish : "Yes, sure!"
property dialogTooMuchFilesNoEnglish : "Hell, no!"
property dialogNothingSelectedMessageEnglish : "Please select one or more files in Finder."
property dialogNothingSelectedButtonEnglish : "Allright, I do whatever you say!"
property dialogErrorOccurredMessageEnglish : "One or more errors occured:" & return & return
property dialogErrorOccurredButtonEnglish : "Doh!"
property dialogChecksumFileMissingMessage01English : "The checksum file for the file \""
property dialogChecksumFileMissingMessage02English : "\" is missing or corrupted!"
property dialogChecksumFileMissingButtonEnglish : "Gee!"
property dialogChecksumMismatchMessage01English : "The checksum for the file \""
property dialogChecksumMismatchMessage02English : "\" does not match!"
#property dialogChecksumMismatchButtonEnglish : "Gee!"
(* Properties (Deutsch) *)
property dialogAttentionWordGerman : "Achtung! "
property dialogErrorWordGerman : "Error: "
property dialogTooMuchFilesMessage01German : "Bist Du sicher, dass Du für "
property dialogTooMuchFilesMessage02German : " Prüfsummen überprüfen willst?"
property dialogTooMuchFilesYesGerman : "Na klar!"
property dialogTooMuchFilesNoGerman : "Bloss nicht!"
property dialogNothingSelectedMessageGerman : "Bitte wähle eine oder mehrere Dateien im Finder aus."
property dialogNothingSelectedButtonGerman : "Okay, mach ich!"
property dialogErrorOccurredMessageGerman : "Ein oder mehr Fehler ist aufgetreten:" & return & return
property dialogErrorOccurredButtonGerman : "Och nö!"
property dialogChecksumFileMissingMessage01German : "Die Prüfsummendatei für die Datei \""
property dialogChecksumFileMissingMessage02German : "\" fehlt oder ist fehlerhaft!"
property dialogChecksumFileMissingButtonGerman : "Mist!"
property dialogChecksumMismatchMessage01German : "Die Prüfsumme für die Datei \""
property dialogChecksumMismatchMessage02German : "\" stimmt nicht überein!"
#property dialogChecksumMismatchButtonGerman : "Gee!"

(* UnMain() *)
on run (argv)
(* Init *)
set logMessage to ""
if (my systemLanguage() is "de") then
set dialogAttentionWord to dialogAttentionWordGerman
set dialogErrorWord to dialogErrorWordGerman
set dialogTooMuchFilesMessage01 to dialogTooMuchFilesMessage01German
set dialogTooMuchFilesMessage02 to dialogTooMuchFilesMessage02German
set dialogTooMuchFilesYes to dialogTooMuchFilesYesGerman
set dialogTooMuchFilesNo to dialogTooMuchFilesNoGerman
set dialogNothingSelectedMessage to dialogNothingSelectedMessageGerman
set dialogNothingSelectedButton to dialogNothingSelectedButtonGerman
set dialogErrorOccurredMessage to dialogErrorOccurredMessageGerman
set dialogErrorOccurredButton to dialogErrorOccurredButtonGerman
set dialogChecksumFileMissingMessage02 to dialogChecksumFileMissingMessage01German
set dialogChecksumFileMissingMessage02 to dialogChecksumFileMissingMessage02German
set dialogChecksumFileMissingButton to dialogChecksumFileMissingButtonGerman
set dialogChecksumMismatchMessage01 to dialogChecksumMismatchMessage01German
set dialogChecksumMismatchMessage02 to dialogChecksumMismatchMessage02German
else
set dialogAttentionWord to dialogAttentionWordEnglish
set dialogErrorWord to dialogErrorWordEnglish
set dialogTooMuchFilesMessage01 to dialogTooMuchFilesMessage01English
set dialogTooMuchFilesMessage02 to dialogTooMuchFilesMessage02English
set dialogTooMuchFilesYes to dialogTooMuchFilesYesEnglish
set dialogTooMuchFilesNo to dialogTooMuchFilesNoEnglish
set dialogNothingSelectedMessage to dialogNothingSelectedMessageEnglish
set dialogNothingSelectedButton to dialogNothingSelectedButtonEnglish
set dialogErrorOccurredMessage to dialogErrorOccurredMessageEnglish
set dialogErrorOccurredButton to dialogErrorOccurredButtonEnglish
set dialogChecksumFileMissingMessage01 to dialogChecksumFileMissingMessage01English
set dialogChecksumFileMissingMessage02 to dialogChecksumFileMissingMessage02English
set dialogChecksumFileMissingButton to dialogChecksumFileMissingButtonEnglish
set dialogChecksumMismatchMessage01 to dialogChecksumMismatchMessage01English
set dialogChecksumMismatchMessage02 to dialogChecksumMismatchMessage02English
end if

(* Get selected files/folders *)
tell application "Finder"
set selectedItems to selection
(* Check wether something selected *)
if ((count of selectedItems) is greater than 0) then
(* walk through items *)
repeat with oneItem in selectedItems
(* Do dry-run to determine file count *)
set fileCount to 0
tell me to checkChecksums(oneItem, true)
log "fileCount: " & fileCount
if (fileCount is greater than 23) then
(* Ask user if he's sure that's what he wants to do *)
display dialog dialogTooMuchFilesMessage01 & fileCount & dialogTooMuchFilesMessage02 buttons {dialogTooMuchFilesYes, dialogTooMuchFilesNo} default button 2
set returnedButton to button returned of result
if (returnedButton is not dialogTooMuchFilesYes) then
tell me to exit repeat #quit
end if
end if
(* Now actually create the checksums *)
set fileCount to 0
tell me to checkChecksums(oneItem, false)
end repeat (* loop through sel. files *)
(* If something went wrong -> tell the user *)
if ((count of characters of logMessage) is greater than 0) then
display dialog dialogErrorOccurredMessage & logMessage buttons {dialogErrorOccurredButton} default button 1 with icon 0 (* icon 0: stop sign, icon 1: App-Icon, icon 2: Att. Sign and App Icon *)
end if
else
display dialog dialogNothingSelectedMessage buttons dialogNothingSelectedButton
end if
end tell (* tell finder *)
end run

(* subroutine for checking md5 checksums *)
on checkChecksums(finderItem, dryRun)
try
(* check wether it's a file or a folder, if file create an additional md5 checksum file with similar name *)
tell application "Finder"
set theItemClass to class of finderItem
set theItemName to name of finderItem
set theItemContainer to (container of finderItem) as alias
set itemPath to POSIX path of (finderItem as alias)
set itemContainerPath to POSIX path of theItemContainer
if (theItemClass is folder) then (* not using type because it's localized, class is not *)
(* do this recursive, now with the contents of this folder *)
set aliasList to every item of finderItem (* use of "every file" would result in a list only containing the files *)
if ((count of aliasList) is greater than 0) then
repeat with oneItem in items of aliasList
tell me to createChecksums(oneItem, dryRun)
end repeat
end if
else
(* Don't check checksums of checksums! *)
if (theItemName does not end with ".md5") then
set fileCount to fileCount + 1
(* If dry-running, only increment file counter *)
if (dryRun is not true) then
(* Check for checksum file *)
set checkSum to ""
set checksumFileName to theItemName & ".md5"
set getChecksumFileContentCommand to "cd \"" & itemContainerPath & "\" && cat \"" & checksumFileName & "\""
try
tell AppleScript to set checkSum to do shell script getChecksumFileContentCommand
on error (e)
log "Error: " & e (* Probably the md5 file is missing or has a fucked-up name *)
end try
log "checksum from .md5-file: " & checkSum
(* Is the read checksum valid? *)
if (length of checkSum is less than 5) then
set logMessage to logMessage & dialogChecksumFileMissingMessage01 & checksumFileName & dialogChecksumFileMissingMessage02 & return
else
(* Create checksum for file *)
set cmd to "cd \"" & itemContainerPath & "\" && md5 -q \"" & theItemName & "\" | head -1"
# log "Creating md5 checksum with this command: " & cmd
tell AppleScript to set theItemsChecksum to do shell script cmd
log "checksum from file: " & theItemsChecksum
(* Compare checksum *)
if (theItemsChecksum is not equal to checkSum) then
set logMessage to logMessage & dialogAttentionWord & dialogChecksumMismatchMessage01 & theItemName & dialogChecksumMismatchMessage02 & " (" & theItemsChecksum & "/" & checkSum & ")" & return & return
log "Checksum does not match: " & theItemName
end if
end if
end if (* if dryRun *)
end if (* if not md5 checksum file *)
end if (* if file *)
end tell (* tell finder *)
on error (e)
set logMessage to logMessage & "Error: " & e & return
end try
end checkChecksums

on systemLanguage()
set cmd to "defaults read NSGlobalDomain AppleLanguages | grep -v \"^(\" | grep -v \"^)\" | head -1 | cut -d, -f1 | awk '{printf $1}'"

try
tell AppleScript to set languageCode to do shell script cmd
on error (e)
log "WTF? Cannot determine language code."
end try

if (languageCode is "de") then
log "Deutsch"
else if (languageCode is "en") then
log "English"
else
log "WTF?"
end if

return languageCode
end systemLanguage





































































































(* nothing here *)
---------------------------------------------------------------------- Dumping ./Copy Selected Mail To Clipboard.scpt (* copies selected mail from apple mail to clipboard as text *)

tell application "Mail"

set theSelectedMessages to selection

if (count theSelectedMessages) is greater than 0 then

repeat with oneSelectedMessage in theSelectedMessages
tell oneSelectedMessage
set t to all headers
set t to t & return & content
log t
tell application "System Events"
set the clipboard to t
end tell
end tell
end repeat

else
display dialog "You haven't selected any message..." buttons {"Doh!"}
end if

end tell
---------------------------------------------------------------------- Dumping ./Create Checksum File.scpt (* Script to create an md5 checksum file at the same location as the checksummed file

ToDo:

- escape quotes in filenames, because that will result in an error
- do the fileCount over all, not only every one selected file (multiple dialogs)
- optional crc using "cksum" instead of md5 (cksum only on Snow Leopard (10.6) and maybe higher)

*)

(* Global Vars *)
global fileCount, logMessage

(* Properties (English) *)
property dialogTooMuchFilesMessage01English : "Are you sure you want to create "
property dialogTooMuchFilesMessage02English : " checksum files?"
property dialogTooMuchFilesYesEnglish : "Yes, sure!"
property dialogTooMuchFilesNoEnglish : "Hell, no!"
property dialogNothingSelectedMessageEnglish : "Please select one or more files in Finder."
property dialogNothingSelectedButtonEnglish : "Allright, I do whatever you say!"
property dialogErrorOccurredMessageEnglish : "One or more errors occured: "
property dialogErrorOccurredButtonEnglish : "Doh!"
(* Properties (Deutsch) *)
property dialogTooMuchFilesMessage01German : "Bist Du sicher, dass Du "
property dialogTooMuchFilesMessage02German : " Prüfsummen-Dateien erzeugen willst?"
property dialogTooMuchFilesYesGerman : "Na klar!"
property dialogTooMuchFilesNoGerman : "Bloss nicht!"
property dialogNothingSelectedMessageGerman : "Bitte wähle eine oder mehrere Dateien im Finder aus."
property dialogNothingSelectedButtonGerman : "Okay, mach ich!"
property dialogErrorOccurredMessageGerman : "Ein oder mehr Fehler ist aufgetreten: "
property dialogErrorOccurredButtonGerman : "Och nö!"

(* UnMain() *)
on run (argv)
(* Init *)
set logMessage to ""
if (my systemLanguage() is "de") then
set dialogTooMuchFilesMessage01 to dialogTooMuchFilesMessage01German
set dialogTooMuchFilesMessage02 to dialogTooMuchFilesMessage02German
set dialogTooMuchFilesYes to dialogTooMuchFilesYesGerman
set dialogTooMuchFilesNo to dialogTooMuchFilesNoGerman
set dialogNothingSelectedMessage to dialogNothingSelectedMessageGerman
set dialogNothingSelectedButton to dialogNothingSelectedButtonGerman
set dialogErrorOccurredMessage to dialogErrorOccurredMessageGerman
set dialogErrorOccurredButton to dialogErrorOccurredButtonGerman
else
set dialogTooMuchFilesMessage01 to dialogTooMuchFilesMessage01English
set dialogTooMuchFilesMessage02 to dialogTooMuchFilesMessage02English
set dialogTooMuchFilesYes to dialogTooMuchFilesYesEnglish
set dialogTooMuchFilesNo to dialogTooMuchFilesNoEnglish
set dialogNothingSelectedMessage to dialogNothingSelectedMessageEnglish
set dialogNothingSelectedButton to dialogNothingSelectedButtonEnglish
set dialogErrorOccurredMessage to dialogErrorOccurredMessageEnglish
set dialogErrorOccurredButton to dialogErrorOccurredButtonEnglish
end if

(* Get selected files/folders *)
tell application "Finder"
set selectedItems to selection
(* Check wether something selected *)
if ((count of selectedItems) is greater than 0) then
(* walk through items *)
repeat with oneItem in selectedItems
(* Do dry-run to determine file count *)
set fileCount to 0
tell me to createChecksums(oneItem, true)
log "fileCount: " & fileCount
if (fileCount is greater than 23) then
(* Ask user if he's sure that's what he wants to do *)
display dialog dialogTooMuchFilesMessage01 & fileCount & dialogTooMuchFilesMessage02 buttons {dialogTooMuchFilesYes, dialogTooMuchFilesNo} default button 2
set returnedButton to button returned of result
if (returnedButton is not dialogTooMuchFilesYes) then
tell me to exit repeat #quit
end if
end if
(* Now actually create the checksums *)
set fileCount to 0
tell me to createChecksums(oneItem, false)
end repeat (* loop through sel. files *)
(* If something went wrong -> tell the user *)
if ((count of characters of logMessage) is greater than 0) then
display dialog dialogErrorOccurredMessage & logMessage buttons {dialogErrorOccurredButton} default button 1
end if
else
display dialog dialogNothingSelectedMessage buttons dialogNothingSelectedButton
end if
end tell (* tell finder *)
end run

(* subroutine for creating md5 checksums *)
on createChecksums(finderItem, dryRun)
try
(* check wether it's a file or a folder, if file create an additional md5 checksum file with similar name *)
tell application "Finder"
set theItemClass to class of finderItem
set theItemName to name of finderItem
set theItemContainer to (container of finderItem) as alias
set itemPath to POSIX path of (finderItem as alias)
set itemContainerPath to POSIX path of theItemContainer
if (theItemClass is folder) then (* not using type because it's localized, class is not *)
(* do this recursive, now with the contents of this folder *)
set aliasList to every item of finderItem (* use of "every file" would result in a list only containing the files *)
if ((count of aliasList) is greater than 0) then
repeat with oneItem in items of aliasList
tell me to createChecksums(oneItem, dryRun)
end repeat
end if
else
(* Don't create checksums of checksums! *)
if (theItemName does not end with ".md5") then
set fileCount to fileCount + 1
(* If dry-running, only increment file counter *)
if (dryRun is not true) then
(* create checksum for file *)
set checksumFileName to theItemName & ".md5"
set cmd to "cd \"" & itemContainerPath & "\" && md5 -q \"" & theItemName & "\" | head -1 > \"" & checksumFileName & "\""
log "Creating md5 checksum with this command: " & cmd
try
tell AppleScript to set cmdRes to do shell script cmd
on error (e)
log "This error occured while creating the checksum file: " & e
end try
log "Creating checksum returned: " & cmdRes
end if (* if dryRun *)
end if
end if (* if file *)
end tell (* tell finder *)
on error (e)
set logMessage to logMessage & "Error: " & e & return
end try
end createChecksums

on systemLanguage()
set cmd to "defaults read NSGlobalDomain AppleLanguages | grep -v \"^(\" | grep -v \"^)\" | head -1 | cut -d, -f1 | awk '{printf $1}'"

try
tell AppleScript to set languageCode to do shell script cmd
on error (e)
log "WTF? Cannot determine language code."
end try

if (languageCode is "de") then
log "Deutsch"
else if (languageCode is "en") then
log "English"
else
log "WTF?"
end if

return languageCode
end systemLanguage





































































































(* nothing here *)
---------------------------------------------------------------------- Dumping ./Dial Selected Number.scpt tell application "System Events" to keystroke "c" using {command down}
delay 0.1
set someword to the clipboard

tell application "direct" to MakeCall to someword as string

---------------------------------------------------------------------- Dumping ./Google Images Selected Text.scpt tell application "System Events" to keystroke "c" using {command down}
delay 0.1
get the clipboard
set someword to the clipboard

tell application "Safari"
activate
open location "http://google.com/images?q='" & someword & "'"
end tell

---------------------------------------------------------------------- Dumping ./Google Selected Text.scpt tell application "System Events" to keystroke "c" using {command down}
delay 0.1
set someword to the clipboard

tell application "Safari"
activate
open location "http://google.com/search?q='" & someword & "'"
end tell
---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/*****.scpt tell application "iTunes"
set r to 5
set t to current track
set rating of t to 100 / 5 * r
end tell
---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/****.scpt tell application "iTunes"
set r to 4
set t to current track
set rating of t to 100 / 5 * r
end tell
---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/***.scpt tell application "iTunes"
set r to 3
set t to current track
set rating of t to 100 / 5 * r
end tell
---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/**.scpt tell application "iTunes"
set r to 2
set t to current track
set rating of t to 100 / 5 * r
end tell
---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/*.scpt tell application "iTunes"
set r to 1
set t to current track
set rating of t to 100 / 5 * r
end tell
---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/Google Album Artwork Of Current Track.scpt tell application "iTunes"
# set tName to name of current track
set tArtist to artist of current track
set tAlbum to album of current track
# set queryText to tArtist & " " & tAlbum
set queryText to "\"" & tArtist & "\" \"" & tAlbum & "\""
end tell

tell application "Safari"
activate
open location "http://google.com/images?q=" & queryText
end tell

---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/Google Lyrics Of Current Track.scpt tell application "iTunes"

set tArtist to artist of current track
set tName to name of current track
--set queryText to "lyrics " & tArtist & " " & tName
set queryText to "lyrics \"" & tArtist & "\" \"" & tName & "\""
set queryText to my replace_chars(queryText, "&", "%26") # see http://www.degraeve.com/reference/urlencoding.php
end tell

tell application "Safari"
activate
open location "http://google.com/search?q=" & queryText
end tell

# see http://forums.macosxhints.com/archive/index.php/t-26666.html
on replace_chars(this_text, search_string, replacement_string)
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 ""
return this_text
end replace_chars

---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/Open Current Track's Lyrics in TextEdit.scpt (* Just for reading - it's more comfy than in iTune's info-window *)
tell application "iTunes"

set l to lyrics of current track

tell application "TextEdit"
activate

set newDoc to make new document
set text of newDoc to l

end tell

end tell
---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/Paste Current iTunes Track with Album.scpt tell application "iTunes"
get player state
if player state is not paused and player state is not stopped then
tell current track
set message to artist & " - " & album & " - " & name
end tell
# set artist to artist of current track
# set album to album of current track
# set title to name of current track
set the clipboard to message
tell application "System Events" to keystroke "v" using {command down}
end if
end tell

---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/Paste Current iTunes Track.scpt tell application "iTunes"
get player state
if player state is not paused and player state is not stopped then
tell current track
set message to artist & " - " & name
end tell
set the clipboard to message
tell application "System Events" to keystroke "v" using {command down}
end if
end tell

---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/Show Current Track's Whole Album in Library.scpt # http://dougscripts.com/itunes/itinfo/info02.php
# http://dougscripts.com/itunes/itinfo/itunes72info.php
tell application "iTunes"
#activate

display dialog "Not Working yet!"
return



set ct to current track
set tName to name of ct
set tArtist to artist of ct
set tAlbum to album of ct
set queryText to tArtist & " " & tAlbum
#KO set albumTracks to search l for tAlbum only albums
#OK set albumTracks to search playlist "Library" for tAlbum only albums
set albumTracks to (every file track of playlist "Library" whose album contains tAlbum and artist contains tArtist)
log albumTracks

# Maybe I can select the Library and the tracks with some dirty tricks
tell application "System Events"
tell process "iTunes"
get window 1
#get every item of window 1
#get outlines of window 1
#get buttons of window 1
#get browsers of window 1
#get drawers of window 1
#get lists of window 1
#get text fields of window 1
# Plenty of UI Elements:
get UI elements of window 1
# Splitter Group?
set sg to splitter group of window 1
get UI elements of sg
set u to UI elements of sg
log u # empty
# Grow Area?
set g to grow area 1 of window 1
get UI elements of g
set u to UI elements of g
log u #empty
# scroll area 1 (the yellow thingy)
set s to scroll area 1 of window 1
get UI elements of s
set u to UI elements of s
log u

end tell
end tell



# Ab hier Bastelbude: #######################

#set t to first item in albumTracks
#log location of t
#set view of (browser window 1) to parent of t

(* geht nicht:
reveal file track id 120028 of library playlist id 84927 of source id 48 *)
# set t to first item in albumTracks
# reveal t

(* geht:
reveal file track id 489116 of user playlist id 488505 of source id 48

*)
# set t to current track
# reveal t



#KO reveal first item in albumTracks

#KO reveal playlist "Library"

#? reveal current track
#KO reveal (first item in albumTracks) of playlist "Library"

#KO reveal first item in albumTracks


(*OK set t to current track
reveal t *)
#OK reveal current track

# log name of view of (browser window 1)
#log name of view of (front browser window)

#
#set view of (browser window 1) to library playlist
#set view of (browser window 1) to library playlist 1

#set view of front browser window to library playlist 1


# set l to library playlist
# log l



# log name of l
#log parent of l
# log source of l


#log browser window
# set view of front browser window to playlist "Library"
#set view of front browser window to library playlist

# set view of front browser window to user playlist "Library" of source "Library"

#set selection to albumTracks
#browser window
end tell


---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/Speak Current Track.scpt tell application "iTunes"
get player state
if player state is not paused and player state is not stopped then
tell current track
set message to "Now playing " & name & " of the album " & album & " from " & artist
end tell
tell AppleScript to say message using "Victoria" (* see http://docs.info.apple.com/article.html?path=AppleScript/2.1/en/as304.html *)
end if
end tell

---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/Volume/Decrease (-) Volume.scpt set whatToDo to "dec" (* can be "inc", "dec", "mute" *)


(* Get the current output volume (on a scale from 1 to 100) and convert it to a scale from 1 to 16 as seen when using the volume keys *)
set currentVolume to output volume of (get volume settings)
set scaledVolume to round (currentVolume / (100 / 16))

if (whatToDo contains "mute") then
set volume with output muted
else
if (whatToDo contains "inc") then
(* Use this code to increase the volume by a certain interval *)
set soundInterval to 1
set scaledVolume to scaledVolume + soundInterval
if (scaledVolume > 16) then
set scaledVolume to 16
end if
else if (whatToDo contains "dec") then
(* OR use this code to decrease the output volume by 1 *)
set scaledVolume to scaledVolume - 1
if (scaledVolume < 0) then
set scaledVolume to 0
end if
else
set scaledVolume to scaledVolume
end if

(* After using one of the above, the volume needs to be set to the new level (before which we must convert it back to the 1..100 scale) *)
set newVolume to round (scaledVolume / 16 * 100)
set volume output volume newVolume

end if
---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/Volume/Increase (+) Volume.scpt set whatToDo to "inc" (* can be "inc", "dec", "mute" *)


(* Get the current output volume (on a scale from 1 to 100) and convert it to a scale from 1 to 16 as seen when using the volume keys *)
set currentVolume to output volume of (get volume settings)
set scaledVolume to round (currentVolume / (100 / 16))

if (whatToDo contains "mute") then
set volume with output muted
else
if (whatToDo contains "inc") then
(* Use this code to increase the volume by a certain interval *)
set soundInterval to 1
set scaledVolume to scaledVolume + soundInterval
if (scaledVolume > 16) then
set scaledVolume to 16
end if
else if (whatToDo contains "dec") then
(* OR use this code to decrease the output volume by 1 *)
set scaledVolume to scaledVolume - 1
if (scaledVolume < 0) then
set scaledVolume to 0
end if
else
set scaledVolume to scaledVolume
end if

(* After using one of the above, the volume needs to be set to the new level (before which we must convert it back to the 1..100 scale) *)
set newVolume to round (scaledVolume / 16 * 100)
set volume output volume newVolume

end if
---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/Volume/Mute Volume.scpt set whatToDo to "dec" (* can be "inc", "dec", "mute" *)


(* Get the current output volume (on a scale from 1 to 100) and convert it to a scale from 1 to 16 as seen when using the volume keys *)
set currentVolume to output volume of (get volume settings)
set scaledVolume to round (currentVolume / (100 / 16))

if (whatToDo contains "mute") then
set volume with output muted
else
if (whatToDo contains "inc") then
(* Use this code to increase the volume by a certain interval *)
set soundInterval to 1
set scaledVolume to scaledVolume + soundInterval
if (scaledVolume > 16) then
set scaledVolume to 16
end if
else if (whatToDo contains "dec") then
(* OR use this code to decrease the output volume by 1 *)
set scaledVolume to scaledVolume - 1
if (scaledVolume < 0) then
set scaledVolume to 0
end if
else
set scaledVolume to scaledVolume
end if

(* After using one of the above, the volume needs to be set to the new level (before which we must convert it back to the 1..100 scale) *)
set newVolume to round (scaledVolume / 16 * 100)
set volume output volume newVolume

end if
---------------------------------------------------------------------- Dumping ./iTunes Currently Playing Track/Volume/Toggle Mute.scpt (* Mute/Unute
*)
set _muted to (get (output muted of (get volume settings)))
if _muted is false then
set volume with output muted
else
set volume without output muted
end if
---------------------------------------------------------------------- Dumping ./iTunes Selected Tracks/*****.scpt tell application "iTunes"
if selection is not {} then -- there ARE tracks selected...

set sel to a reference to selection
set trackCount to count sel
if trackCount is greater than 23 then
set msg to "Are you sure, you want to rate " & trackCount & " tracks?"
set res to display dialog msg buttons {"Sure!", "Actually, no."}
if button returned of res is not "Sure!" then
return
end if
end if

repeat with aTrack in sel

set r to 5
set rating of aTrack to 100 / 5 * r

end repeat
end if
end tell
---------------------------------------------------------------------- Dumping ./iTunes Selected Tracks/****.scpt tell application "iTunes"
if selection is not {} then -- there ARE tracks selected...

set sel to a reference to selection
set trackCount to count sel
if trackCount is greater than 23 then
set msg to "Are you sure, you want to rate " & trackCount & " tracks?"
set res to display dialog msg buttons {"Sure!", "Actually, no."}
if button returned of res is not "Sure!" then
return
end if
end if

repeat with aTrack in sel

set r to 4
set rating of aTrack to 100 / 5 * r

end repeat
end if
end tell
---------------------------------------------------------------------- Dumping ./iTunes Selected Tracks/***.scpt tell application "iTunes"
if selection is not {} then -- there ARE tracks selected...

set sel to a reference to selection
set trackCount to count sel
if trackCount is greater than 23 then
set msg to "Are you sure, you want to rate " & trackCount & " tracks?"
set res to display dialog msg buttons {"Sure!", "Actually, no."}
if button returned of res is not "Sure!" then
return
end if
end if

repeat with aTrack in sel

set r to 3
set rating of aTrack to 100 / 5 * r

end repeat
end if
end tell
---------------------------------------------------------------------- Dumping ./iTunes Selected Tracks/**.scpt tell application "iTunes"
if selection is not {} then -- there ARE tracks selected...

set sel to a reference to selection
set trackCount to count sel
if trackCount is greater than 23 then
set msg to "Are you sure, you want to rate " & trackCount & " tracks?"
set res to display dialog msg buttons {"Sure!", "Actually, no."}
if button returned of res is not "Sure!" then
return
end if
end if

repeat with aTrack in sel

set r to 2
set rating of aTrack to 100 / 5 * r

end repeat
end if
end tell
---------------------------------------------------------------------- Dumping ./iTunes Selected Tracks/*.scpt tell application "iTunes"
if selection is not {} then -- there ARE tracks selected...

set sel to a reference to selection
set trackCount to count sel
if trackCount is greater than 23 then
set msg to "Are you sure, you want to rate " & trackCount & " tracks?"
set res to display dialog msg buttons {"Sure!", "Actually, no."}
if button returned of res is not "Sure!" then
return
end if
end if

repeat with aTrack in sel

set r to 1
set rating of aTrack to 100 / 5 * r

end repeat
end if
end tell
---------------------------------------------------------------------- Dumping ./iTunes Selected Tracks/Google Album Artwork Of Selected Track.scpt tell application "iTunes"
if selection is not {} then -- there ARE tracks selected...
set sel to a reference to selection
if ((count of sel) > 1) then
activate
set res to display dialog "Are you sure you want Artwork for more than one track?" buttons {"I know what I'm doing", "Ah, no, thank you!"}
#if
log res
if button returned of res is not "I know what I'm doing" then
return
end if
end if
repeat with aTrack in sel
set tArtist to artist of aTrack
set tAlbum to album of aTrack
set queryText to "\"" & tArtist & "\" \"" & tAlbum & "\""
tell application "Safari"
activate
open location "http://google.com/images?q=" & queryText & ""
end tell
end repeat
end if
end tell


---------------------------------------------------------------------- Dumping ./iTunes Selected Tracks/Google Lyrics Of Selected Track.scpt tell application "iTunes"

if selection is not {} then -- there ARE tracks selected...

set sel to a reference to selection
set trackCount to count sel
if trackCount is greater than 1 or trackCount is less than 1 then
activate
set msg to "You selected " & trackCount & " tracks. Please select exactly one. Thanks a bunch."
set res to display dialog msg buttons {"Sure!"}
return
end if

set aTrack to first item in sel

set tArtist to artist of aTrack
set tName to name of aTrack
set queryText to "lyrics \"" & tArtist & "\" \"" & tName & "\""
set queryText to my replace_chars(queryText, "&", "%26") # see http://www.degraeve.com/reference/urlencoding.php

tell application "Safari"
activate
# open location "http://google.com/search?q='" & queryText & "'"
open location "http://google.com/search?q=" & queryText & ""
end tell

end if

end tell


# see http://forums.macosxhints.com/archive/index.php/t-26666.html
on replace_chars(this_text, search_string, replacement_string)
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 ""
return this_text
end replace_chars

---------------------------------------------------------------------- Dumping ./iTunes Selected Tracks/Paste Selected iTunes Tracks with Album.scpt tell application "iTunes"

if selection is not {} then -- there ARE tracks selected...

set sel to a reference to selection
set trackCount to count sel
if trackCount is greater than 23 then
set msg to "Are you sure, you want to do this with the selected " & trackCount & " tracks?"
set res to display dialog msg buttons {"Sure!", "Actually, no."}
if button returned of res is not "Sure!" then
return
end if
end if

set message to ""
repeat with aTrack in sel

tell aTrack
set message to message & artist & " - " & album & " - " & name & return
end tell

end repeat

set the clipboard to message

tell application "System Events" to keystroke "v" using {command down}

end if
end tell

---------------------------------------------------------------------- Dumping ./iTunes Selected Tracks/Paste Selected iTunes Tracks.scpt tell application "iTunes"

if selection is not {} then -- there ARE tracks selected...

set sel to a reference to selection
set trackCount to count sel
if trackCount is greater than 23 then
set msg to "Are you sure, you want to do this with the selected " & trackCount & " tracks?"
set res to display dialog msg buttons {"Sure!", "Actually, no."}
if button returned of res is not "Sure!" then
return
end if
end if

set message to ""
repeat with aTrack in sel

tell aTrack
set message to message & artist & " - " & name & return
end tell

end repeat

set the clipboard to message

tell application "System Events" to keystroke "v" using {command down}

end if
end tell

---------------------------------------------------------------------- Dumping ./iTunes Selected Tracks/Play Single Track.scpt (*
Ever wondered how you can play a single track? Think of it: You navigate to a track in your music library, and you want to play it. Just this one.
Yes: iTunes offers no method of doing this (except creating a new playlist, adding the track, playing the track und deleting the playlist afterwards. Yes, this sucks).

The bad news: I have no better solution.
Now the good news: You don't have to do it yourself! Here's the script that does it all for you: (place into ~/Library/iTunes/Scripts/ or ~/Library/Scripts/)
*)

global tempplaylistname
on run
(* Init *)

set tempplaylistname to "temp"

(* Create new playlist, if not existing *)
tell me to set pl to newtempplaylist()

(* selected track -> pl *)
tell application "iTunes"

(* remember old playlist *)
set oldpl to view of (browser window 1)

(* Now the magic... *)
try
get class of selection
# log selection
# if ((count of selection) is greater than 0) then
set t to first item in selection

(* track into playlist *)
duplicate t to pl

(* playlist repeat off *)
set song repeat of pl to off

(* playlist to front *)
try
#set view of front window to playlist pl
#set view of browser window to pl
#set view of (browser window 1) to user playlist pl (*Fuck! (iTunes got an error: A descriptor type mismatch occurred.)*)
set view of (browser window 1) to pl (* Works with iTunes 9 *)
on error (e)
log "Fuck! (" & e & ")"
end try

(* play track *)
play first item of tracks of pl

(* Wait until track played or stopped *)
set dur to duration of t
repeat dur times
if player state is not playing then exit repeat
delay 1.0
end repeat

(* restore old playlist *)
set view of (browser window 1) to oldpl
on error (e)
display dialog "Select something, you weenie!" buttons "Yes, your highness!"
end try

(* delete temp playlist *)
delete pl
end tell
end run

(* Create a temp playlist we will delete afterwards *)
on newtempplaylist()
tell application "iTunes"
set pl to make new user playlist with properties {name:tempplaylistname & " as of " & (current date) as string}
end tell
return pl
end newtempplaylist
---------------------------------------------------------------------- Dumping ./iTunes Selected Tracks/Remove Leading "01" from Title.scpt tell application "iTunes"

tell application "iTunes"
if selection is not {} then -- there ARE tracks selected...
set sel to a reference to selection
repeat with aTrack in sel

set oldName to name of aTrack
log "Processing :" & oldName

set cmd to "echo \"" & oldName & "\" | sed s/01\\ //\\g"
tell application "Finder"
set newName to do shell script cmd
end tell
log "Setting new name: " & newName

set name of aTrack to newName

end repeat
end if
end tell

end tell
---------------------------------------------------------------------- Dumping ./Leo Selected Text.scpt (* Copy selected string to clipboard *)
tell application "System Events"
(* Try using Cmd-C *)
set oldClipboard to the clipboard
keystroke "c" using {command down}
set someword to the clipboard
if (oldClipboard is equal to someword) then
log "Most likely you held some more modifiers down (f.e. if you launch this script with FastScripts and a Shortcut that uses option, control or shift..)"
(* Trying to invoke "Copy" Menu command *)
set appName to name of current application
# my menu_click({appName, my menuEditLocalized(), my menuItemEditCopyLocalized(), ""})
tell application "System Events"
set procs to every process
repeat with p in procs
if frontmost of p is true then
set proc to p
end if
end repeat
#set proc to items of processes whose frontmost is true
#set proc to item 1 of processes
get proc
#click menu item "Copy" of ((proc)'s (menu bar 1)'s (menu bar item "Edit"))
set menuBar to menu bar 1 of proc
set editMenu to menu bar item whose name is (my menuEditLocalized())
return 0
end tell
end if
#delay 0.5
end tell


(* Open Browser with query *)
tell application "Safari"
activate
open location "http://dict.leo.org/?search='" & someword & "'"
end tell


(* get localized copy menu item name *)
on menuItemEditCopyLocalized()
tell AppleScript
set cmd to "defaults read NSGlobalDomain AppleLanguages | grep -v \"^(\" | grep -v \"^)\" | head -1 | cut -d, -f1 | awk '{printf $1}'"
set languageCode to do shell script cmd
if (languageCode is "de") then
return "Kopieren"
else if (languageCode is "en") then
return "Copy"
else
log "WTF?"
return ""
end if
end tell
end menuItemEditCopyLocalized


(* get localized copy menu name *)
on menuEditLocalized()
tell AppleScript
set cmd to "defaults read NSGlobalDomain AppleLanguages | grep -v \"^(\" | grep -v \"^)\" | head -1 | cut -d, -f1 | awk '{printf $1}'"
set languageCode to do shell script cmd
if (languageCode is "de") then
return "Bearbeiten"
else if (languageCode is "en") then
return "Edit"
else
log "WTF?"
return ""
end if
end tell
end menuEditLocalized




(* This came from http://www.macosxhints.com/article.php?story=20060921045743404 *)
on menu_click(mList)
local appName, topMenu, r

-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"

-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)

-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
local f, r

-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)

-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menu_click_recurse
---------------------------------------------------------------------- Dumping ./Look Up Selected Text In Dictionary.scpt (* Unfortunately Dictionary is not scriptable but can only be automated with Automator. However there is always the dirty way using System Events. That's what I did to script Dictionary.
*)

(* Get selected text *)
tell application "System Events" to keystroke "c" using {command down}

(* Bring Dictionary to foreground *)
tell application "Dictionary" to activate


(* Paste selected text *)
tell application "System Events" to keystroke "v" using {command down}


(* Look up *)
# tell application "System Events" to keystroke return



---------------------------------------------------------------------- Dumping ./Paste Log Timestamp.scpt on run (argv)
# set oldClip to the clipboard

set the clipboard to my timestamp()


tell application "System Events" to keystroke "v" using {command down}

# set the clipboard to oldClip
end run


(* get a time stamp *)
on timestamp()
set dateNow to current date
set dateYear to year of dateNow
set dateMonth to addLeadingZeros(2, (month of dateNow as number))
set dateDay to addLeadingZeros(2, day of dateNow)
set dateHour to addLeadingZeros(2, hours of dateNow)
set dateMinute to addLeadingZeros(2, minutes of dateNow)
set dateString to (dateYear & "-" & dateMonth & "-" & dateDay & ", " & dateHour & ":" & dateMinute & ", hk: ") as string
end timestamp

(* Liefert von einer Zahl n leading zeros zurück *)
on addLeadingZeros(zeroesCount, value)

(* nimmt die letzten zeroesCount Stellen des Strings *)
set newNumber to (text -zeroesCount thru -1 of ("0000000000000000" & value)) (* Da musch drauf kommen: http://macscripter.net/viewtopic.php?id=24533 *)
log "newNumber: " & newNumber

return (newNumber)
end addLeadingZeros


---------------------------------------------------------------------- Dumping ./Paste Timestamp.scpt set the clipboard to my timestamp()


tell application "System Events" to keystroke "v" using {command down}



(* get a time stamp *)
on timestamp()
set dateNow to current date
set dateYear to year of dateNow
set dateMonth to addLeadingZeros(2, (month of dateNow as number))
set dateDay to addLeadingZeros(2, day of dateNow)
set dateHour to addLeadingZeros(2, hours of dateNow)
set dateMinute to addLeadingZeros(2, minutes of dateNow)
set dateString to (dateYear & "-" & dateMonth & "-" & dateDay & " " & dateHour & ":" & dateMinute) as string
end timestamp

(* Liefert von einer Zahl n leading zeros zurück *)
on addLeadingZeros(zeroesCount, value)

(* nimmt die letzten zeroesCount Stellen des Strings *)
set newNumber to (text -zeroesCount thru -1 of ("0000000000000000" & value)) (* Da musch drauf kommen: http://macscripter.net/viewtopic.php?id=24533 *)
log "newNumber: " & newNumber

return (newNumber)
end addLeadingZeros

---------------------------------------------------------------------- Dumping ./Reply Mail With Quote.scpt (* History:
2010-02-21, 16:44, hk: First version.
* If name contains "," reverse order!--DONE
* get original text--DONE
* check if already quoted, if not-> quote--DONE
* Leere Zeilen auch quoten--DONE
* Reply & set new content--DONE

Bug: Erkennung des schon gequoteten buggt!--?

ToDo:

*)


tell application "Mail"
# tell message viewer
# get selected messages
# end tell
set selectedMessages to selection
set selectedMessagesCount to (count selectedMessages)
if selectedMessagesCount > 0 then
if selectedMessagesCount > 23 then
set res to display dialog "Are you sure you want to reply on " & selectedMessagesCount & " messages?" buttons {"No", "Maybe", "Yes"}
if button returned of res is not "Yes" then
return
end if
end if
(* Now, do the magic! *)
repeat with selectedMessage in selectedMessages
tell selectedMessage
set smSender to sender
(* cut the email-address *)
tell me to set smSenderName to cutString of smSender by "<"
(* If name contains "," cut the part before the "," and add it at the end (this way "Doe, John" will get "JD" instead of "DJ") *)
if smSenderName contains "," then
log "foo"
tell me to set smSenderNameSurname to cutString of smSenderName by ","
log smSenderNameSurname
tell me to set smSenderNameFirstname to cutStringRemainder of smSenderName by ","
log smSenderNameFirstname
set smSenderName to (smSenderNameFirstname & " " & smSenderNameSurname) as text (* yes, that's ugly *)
log smSenderName
end if
(* get initials of the name *)
tell me to set smInitials to initialsOfName(smSenderName)
(* get text of mail and walk lines... http://www.mactech.com/articles/mactech/Vol.21/21.07/WorkingWithText/index.html *)
set smText to content
set smTextQuoted to ""
#log smText
#set smTextLineCount to count paragraphs of smText

(* Zeilen (=Paragraphs) abklappern *)
repeat with p in (paragraphs of smText)

(* Get line length *)
set lineLength to (length of p)
if lineLength > 5 then
set lineLength to 5
end if

if lineLength > 0 then
set pBegin to characters 1 thru lineLength of p
else
set pBegin to ""
end if

if pBegin contains ">" then
(* is quoted -> just concatenate strings *)
#log "already quoted: " & pBegin
set smTextQuoted to smTextQuoted & (p as string) & return
else

#if pBegin does not contain ">" then
(* not quoted yet, quote! *)
set smTextQuoted to smTextQuoted & smInitials & "> " & (p as string) & return
#else
# (* already quoted *)
# set smTextQuoted to smTextQuoted & (p as string) & return
#end if
#else
#set smTextQuoted to smTextQuoted & (p as string) & return
end if

end repeat (* repeat with paragraphs *)
log "quoted text: " & return & smTextQuoted
end tell # tell selectedMail

(* Backup quoting behaviour *)
tell application "Mail" to set quotingSetting to quote original message
tell application "Mail" to set quote original message to false

(* now reply *)
set newMessage to reply selectedMessage with opening window and reply to all
if newMessage is false then
log "Gee, reply failed."
else
tell newMessage
log newMessage
#set content of newMessage to "foo" -- smTextQuoted
# make new content with properties {text:"smTextQuoted"}
log (content)
log (subject)
log (sender)
log (to recipients)
#make new to recipient with properties {address:"MY ADDRESS"}
log (to recipients)
#make new content with t
#log "s: " & (contents as text)
set content to "foo"
set content to smTextQuoted
#make new content with properties {text:"MY text"}
end tell
end if # reply worked

(* Restore quoting behaviour *)
tell application "Mail" to set quote original message to quotingSetting

end repeat (* repeat with mails *)
else
log ("You might want to select a message first")
end if
end tell

(* Now get the first characters of every word (Initials) *)
on initialsOfName(sendername)
set initials to ""
repeat with oneWord in (words of sendername)
set initials to initials & first character of oneWord
end repeat
return initials
end initialsOfName

(* cuts all the text following the first occurance of the searchstring 'd' *)
to cutString of t by d
set oldd to text item delimiters
set text item delimiters to d
set t to t's text items
set t to first item in t
set text item delimiters to oldd
t
end cutString

(* returns what cutString has cut off! *)
to cutStringRemainder of t by d
set oldd to text item delimiters
set text item delimiters to d
set t to t's text items
set wordCount to count t
if (wordCount > 1) then
set t to items 2 through wordCount in t
end if
set text item delimiters to oldd
t
end cutStringRemainder

(* Diese Funktion ersetzt einen String innerhalb eines strings durch einen anderen String. Quelle: http://macscripter.net/viewtopic.php?id=13008 *)
to changeSubString of t from s to r
set d to text item delimiters
set text item delimiters to s
set t to t's text items
set text item delimiters to r
tell t to set t to beginning & ({""} & rest)
set text item delimiters to d
t
end changeSubString

HeiMac:Little Helpers Scripts heiko$