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

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

Addressbuch: Einzelne vCards exportieren

6/02/2011
Wenn man im Apple Adressbuch Kontakte als vCard exportieren möchte, erstellt das Programm bei mehreren Kontakten anscheinend immer nur eine Datei, in der alle Informationen stehen... FALSCH, mit gedrückter alt-Taste erstellt das Programm auf Wunsch auch separate VCF-Dateien für jeden Kontakt (siehe Kommentare).... somit kann man sich das Weiterlesen eigentlich sparen...Nun, dieses Skript (quick&dirty) exportiert die Kontakte in einzelne .vcf Dateien

-- hubionmac.com 06.02.2011

-- quick&dirty script for exporting contacts as single vCards

--exisiting vCards will not be overwritten by the script, instead new files will 

--get an index like "John Smith 1.vcf"


set doThese to get_item_2_process(true)

tell application "Address Book"

set destination_path to choose folder with prompt "Destination folder for export:"

repeat with doThis in doThese

if first name of doThis is missing value then

set firstname to ""

else

set firstname to first name of doThis

end if

if last name of doThis is missing value then

set lastname to ""

else

set lastname to last name of doThis

end if

if (lastname & firstname) as text = "" then

set namestring to "nobody.vcf"

else

set namestring to (first name of doThis & " " & last name of doThis & ".vcf") as text

end if

set namestring to my checkname_with_pdf_suffix(namestring, destination_path, false)

my writeToFile((destination_path as text) & namestring as text, (get vcard of doThis), false)

end repeat

my display_message(((count of doThese) & " contact(s) exported") as text, 3)

end tell





on writeToFile(MacFilePathTxt, txt, add2eof)

--lastedit 18.01.2011

if add2eof is false then

try

do shell script "rm " & quoted form of POSIX path of (MacFilePathTxt as alias)

end try

end if

set RefNum to (open for access file MacFilePathTxt with write permission)

try

if add2eof is false then

write txt to RefNum

else

write txt to RefNum starting at ((get eof RefNum) + 1)

end if

close access RefNum

return true

on error

close access RefNum

return false

end try

end writeToFile



to get_item_2_process(askwhat)

try

tell application "Address Book"

activate

tell application "System Events"

keystroke "0" using command down --show address-window not matter what...

end tell

if askwhat is true then

set theaction to button returned of (display dialog "Process all or just selection?" buttons {"All", "Selection", "cancel"} default button {"Selection"})

if theaction = "Selection" then

set a to every person whose selected is true

else

set a to people

end if

else

set a to people

end if

end tell

on error msg

error "error on get_item_2_process" & return & msg

end try

end get_item_2_process



to checkname_with_pdf_suffix(n, D, looped)

--check if filename exists in D

-- so if "A File.pdf" exists it names it "A File 1.pdf","A File 2.pdf",...

tell application "Finder"

set thefiles to name of every item of (D as alias)

end tell

if thefiles contains n then

if looped = false then

set n to ((characters 1 through -5 of n) & " 1" & (characters -4 through -1 of n)) as text

my checkname_with_pdf_suffix(n, D, true)

else

set tmp to (last word of ((characters 1 through -5 of n) as text) as integer)

set tmpcount to (count of characters of (tmp as text)) + 5

set tmp to tmp + 1

set n to ((characters 1 through (-1 * tmpcount) of n) & tmp & (characters -4 through -1 of n)) as text

my checkname_with_pdf_suffix(n, D, true)

end if

else

return n

end if

end checkname_with_pdf_suffix


on display_message(msgTXT, msgTimeout)

tell application "System Events"

set isRunning to ¬

(count of (every process whose name is "GrowlHelperApp")) > 0

end tell

if isRunning = true then

tell application "GrowlHelperApp"

-- Make a list of all the notification types 

-- that this script will ever send:

set the allNotificationsList to ¬

{"Status"}

-- Make a list of the notifications 

-- that will be enabled by default.      

-- Those not enabled by default can be enabled later 

-- in the 'Applications' tab of the growl prefpane.

set the enabledNotificationsList to ¬

{"Status"}

-- Register our script with growl.

-- You can optionally (as here) set a default icon 

-- for this script's notifications.

register as application ¬

"Address Book" all notifications allNotificationsList ¬

default notifications enabledNotificationsList ¬

icon of application "Address Book"

--Send a Notification...

notify with name ¬

"Status" title ¬

"Status" description ¬

msgTXT application name ¬

"Address Book"

return true

end tell

else

activate

display dialog msgTXT giving up after msgTimeout

end if

end display_message

2 Comments

AppleScript: Excel -> Photoshop

20/01/2011

Von Zeit zur Zeit muss ich mal in Photoshop Video-Tafeln setzen. Die Daten hierfür kommen meistens aus Excel und werden natürlich per AppleScript in eine Vorlage übertragen =)
Das Skript sollte sowohl mit Photoshop Elements als auch mit der nicht abgespeckten Version funktionieren.

Read the rest of this article »
No Comments

iCal: Importiere abonnierten Kalender in Lokalen…

7/11/2010
Code zum markieren einmal anklicken Code im Skript-Editor öffnen

tell application "iCal"

activate

##wähle eine Kalender aus und ermittle seine UID

## dies ist auch der Ordnername im ~/Library/Calendars in dem die plist mit

## der URL des Kalenders liegt

set callist to (name of every calendar whose writable is false)

set callist2 to (name of every calendar whose writable is true)

set callToExport to (choose from list callist default items (item 1 of callist) with prompt "Exportiere…:")

set callToImport to (choose from list callist2 default items (item 1 of callist2) with prompt "Importiere in…:")

if callToExport is not false and callToImport is not false then

set callUID to uid of calendar (callToExport as text)

##nun die URL des Kalenders auslesen

set myurl to (do shell script "defaults read ~/Library/Calendars/" & callUID & ".calendar/Info SubscriptionURL")

if myurl starts with "webcal" then

set myurl to "http" & (characters 7 through -1 of myurl) as text

end if

##sollte ein kennwort für den Download notwendig sein:

## set myusername to "username"

## set mypassword to "geheim"

## set myurl to "http://" & myusername & ":" & mypassword & "@" & (characters 8 through -1 of myurl) as text

#####

do shell script "cd /tmp;curl " & myurl & " > " & quoted form of (callToExport as text) & ".ics;open " & quoted form of (callToExport as text) & ".ics"

delay 1

activate application "iCal"

tell application "System Events"

tell process "iCal"

click pop up button 1 of window "Ereignisse hinzufügen"

keystroke (callToImport as text)

delay 0.25

keystroke return

delay 0.25

keystroke return

end tell

end tell

end if

end tell


No Comments

csv Datei um unnötige Spalten erleichtern

21/04/2010
Exports aus Datenbanken kommen ja gerne als CSV-Datei daher, diese sind mit Kommas separiert, manchmal sind aber in den Werten selber auch Kommas also z.B. "hubi","hubionmac","90,95€",22 Das Problem ist also, dass man nicht einfach an Hand der , die Zeile aufsplitten kann, sondern auch darauf achten muss, ob ein Komma in Anführungszeichen eingeschlossen ist oder nicht.
Ich wollte dafür ursprünglich ein kleines Ruby-Skript schreibe, scheiterte aber an meinen noch recht bescheidenen Kenntnissen von passenden regulären Ausdrücken.
Damit aber nun jetzt zumindest schon mal eine halbwegs schnelle Lösung (die könnte man auch noch etwas verbessern, gerade was das schreiben der Datei angeht) da ist, habe ich dieses Script geschrieben, welches sich einer etwas längeren awk-Zeile bedient, um die Werte einer solchen CSV-Datei auszulesen. Man wählt die CSV-Datei aus, gibt des Skript auf dem Weg noch die Spalten mit, die man haben möchte und schon wird ein neue CSV-Datei mit den gewünschten Spalten-Werten erstellt und geöffnet.
Den Quellcode bekomme ich leider ums Verrecken hier nicht fehlerfrei gepostet, da in dem awk-Kommando anscheinend deutlich zu viele Sonderzeichen auftauchen ;-P
Die Datei Skript-Datei also hier zu Download:
csv convert.scpt
1 Comment

iPhoto: RAW2Sonstwas-Converter

9/11/2009
iPhoto bittet eingentlich schon nette Import- und Export-Funktionen, wenn man aber mal Platz sparen und seine ganzen RAW-Dateien durch platzsparende JPEGs oder PNGs ersetzten und die RAWs danach löschen möchte... nun, da greift dieses Skript an.
  1. Es konvertiert die ausgewählten Bilder in iPhoto in ein anderes Format
  2. verkleinert die Bilder ggf.
  3. kopiert sich die Infos vom Original (Name, GPS-Daten, Datum etc.)
  4. importiert die neue Kopie in iPhoto
  5. setzt die gespeicherte Infos bei der impierten Kopie
  6. und LÖSCHT das zu Anfang ausgewählte Original
Ob das nun wirklich der Ideale Weg ist, bleibt mal dahingestellt, da die RAWs einfach mit den Standard-Einstellungen umgerechnet werden, deshalb könnte sicherlich die ein oder andere Bildinformation auf der Strecke bleiben. Es wurde aber so gewünscht und es ist ein schönes Beispiel, wie man iPhoto skripten kann =) Also viel Spaß beim Ausprobieren.
Code zum markieren einmal anklicken Code im Skript-Editor öffnen

-- hubionmac.com 2009-09-27

-- convertes selected images in iPhoto 

--  (format PNG,TIFF,JPEG),

--  resizes them (place 0 as new width to get orig. size)

-- imports them into iphoto

-- copies info like date, name, rating to new converted version

-- tested with Mac OS 10.6 and iPhoto 8.1


tell application "iPhoto"

--Get selected images

set myPhotos to selection

--init some lists

set newfiles to {}

set newfiles_record to {}

--make a new album for the converted images

set myAlbum to my set_Album((do shell script "date '+Converted at %Y-%m-%d %H:%M:%S'"))

--Loop through each image

repeat with p in myPhotos

--make a unique string, so images are not overwritten

set current_time_md5 to "hubionmac_iphoto_temp_" & (do shell script "date | md5")

-- now this is 2 in 1 line

-- first it converts the current image 

--second it stores the converted image's path in a list

set newfiles to newfiles & {(my convert_image((POSIX file (image path of p)) as alias, (POSIX file ("/tmp/") & current_time_md5) as string, 1024, "JPEG"))}

--this is an info record so later on the converted image gets the same informations (name, date, rating, etc)

set newfiles_record to newfiles_record & {{new_name:current_time_md5, l:(latitude of p), ll:(longitude of p), myComment:(comment of p), myName:(name of p), myTitle:(title of p), myRating:(rating of p), mydate:(date of p)}}

--label the orig_image, by adding _was_converted to the image's name

if name of p does not end with "_was_converted" then

set name of p to name of p & "_was_converted"

end if

end repeat

-- import all converted images into iphoto in a new album

import from newfiles to album

-- do-nothing-loop until iPhoto imported all images....

repeat until (importing) is false

delay 5

end repeat

-- copy infos like name, date, rating to the corresponding "new" image....

my post_process(newfiles_record, myAlbum)

end tell


on post_process(newfiles_record, myAlbum)

repeat with r in newfiles_record

tell application "iPhoto"

set a to photo ((new_name of r) as text) of album myAlbum

set latitude of a to (l of r)

set longitude of a to (ll of r)

set comment of a to (myComment of r)

set name of a to (myName of r) & "_converted"

set title of a to (myTitle of r)

set rating of a to (myRating of r)

set date of a to (mydate of r)

end tell

end repeat

do shell script "rm /tmp/hubionmac_iphoto_temp_*"

end post_process


on convert_image(image_file, target_path, target_width, target_format)

tell application "Image Events"

launch

-- open the image file

set this_image to open image_file

if target_width > 0 then

copy dimensions of this_image to {current_width, current_height}

if current_width is greater than current_height then

scale this_image to size target_width

else

set new_height to (target_width * current_width) / current_height

scale this_image to size new_height

end if

end if

if target_format = "PNG" then

set target_path to target_path & "." & target_format

save this_image in target_path as PNG

else if target_format = "JPEG" then

set target_path to target_path & "." & target_format

save this_image in target_path as JPEG

else if target_format = "JPEG2" then

set target_path to target_path & ".JPEG"

save this_image in target_path as JPEG2

else if target_format = "TIFF" then

set target_path to target_path & "." & target_format

save this_image in target_path as TIFF

end if

end tell

return target_path as alias

end convert_image



on set_Album(albumname)

tell application "iPhoto"

if album albumname exists then

return albumname

else

new album name albumname

return albumname

end if

end tell

end set_Album

No Comments