PDF: Dynamisches Wasserzeichen mit SVG->PDF Overlay

11/12/2011

Ich habe einen neuen Trick gefunden automatisch PDFs zu erstellen, ohne dabei auf allzu teure (Indesign, Quark), komplizierte (ruby pawn z.B.) oder nicht gerade performante Tools (Pages, Word) angewiesen zu sein. SVG-Dateien sind quasi nichts anderes als XML bzw. Text-Dateien, und lassen sich leicht per Skript manipulieren. Zudem lassen sich auch exakte Format-Angabe (A4, Letter usw.) definieren.

Das Wandeln von SVG in PDF übernimmt dann Inkscape und zwar als Shell-Anwendung. Auf diese Art lassen sich schnell (ohne GUI) PDFs erstellen, die anders als bei der wkpdf-Lösung auch über einen Transparente Hintergrund verfügen.
Ich habe jetzt kein direktes Beispiel zur Hand, verweise aber mal /Applications/Inkscape.app/Contents/Resources/bin/inkscape-bin --help

1 Comment

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

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

AppleScript: Alternative zu ImageEvents

12/03/2010
Anstatt die ImageEvents via AppleScript anzusteuern, kann man auch über ein Shell Script (das dürfte vielleicht schneller sein) auch sips (scriptable image processing system) aufrufen. Damit kann man sogar die Auflösung von Grafiken in PDFs verändern: sips -s format pdf -s dpiHeight 72 -s dpiWidth 72 -s formatOptions low /path/to/pdf.pdf --out /path/to/converted/pdf.pdfSo ist z.B. auch eine deutlich schlankere Version von diesem Skripte (Bilder klein rechnen mit AppleScript) möglich:
Code zum markieren einmal anklicken Code im Skript-Editor öffnen

on open some_items

tell me to activate

display dialog "Längste Seite" default answer "1024"

set mh to (text returned of the result) as integer

repeat with this_item in some_items

rescale_and_save(this_item, mh)

end repeat

end open

to rescale_and_save(this_item, max_length)

set this_item to quoted form of (POSIX path of this_item)

set scaled_dir_path to (do shell script "dirname " & this_item) & "/_scaled/"

do shell script "mkdir -p '" & scaled_dir_path & "';sips -Z " & max_length & " " & this_item & " --out '" & scaled_dir_path & quoted form of (do shell script "basename " & this_item) & "'"

end rescale_and_save

No Comments

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

Website to Text

10/10/2009
Schon interessant, was Mac OSX so für praktische Tools bereit hält. mit textutil lassen sich Texte konvertieren, docx zu html, rtf zu text oder wie im Beispiel der Text-Inhalt einer Webseite zu Text:
Code zum markieren einmal anklicken Code im Skript-Editor öffnen

set web_url to quoted form of "http://hubionmac.com/wordpress/"

do shell script "curl " & web_url & " | textutil -stdin -convert txt -format html -stdout"

No Comments