AppleScript Reste-Eintopf II

9/12/2011

Add to Login-Items

--03.09.2008 hubionmac.com

--asks for an app and adds it to login items

addtologin(choose file of type {"APPL"})

on addtologin(thisApp) -- adds an item to login items

set appPath to POSIX path of thisApp

tell application "System Events"

set appName to name of thisApp

set shortName to (characters 1 through ((get offset of ".app" in appName) - 1) of appName) as text

if shortName is not in (name of every login item) then

make login item at end with properties {path:appPath}

end if

end tell

end addtologin

Nach Datei-Endungen sortieren

Das hatte ich mal als Ordner-Aktion für meinen Download-Ordner erdacht... seitdem es Spotlight gibt, hat der Drang Downloads zu sortieren deutlich nachgelassen ;-)

--19.11.2006 hubionmac.com

--Ordneraktion die Dateien an Hand Ihrer Datei-Endung in Unter-Ordner sortiert

on adding folder items to derOrdner after receiving added_items

-- die Liste kann man gut erweitern

set endungmitordner to {".jpg;JPEGS", ".mov;Movies"}

repeat with k in endungmitordner

--damit kann man die Einträge aus der Liste gut in Ihre Bestandteile zerlegen

--Ist auch super um in Strings Teile zu ersetzen =)

set AppleScript's text item delimiters to ";"

set endung to text item 1 of k

set ordnername to text item 2 of k

set AppleScript's text item delimiters to ""

tell application "Finder"

set inhalt to every item of derOrdner

set itemcount to count of every item of inhalt

-- wenn in dem Ordner nix drin ist, soll er auch nix machen... lohnt ja nicht

if itemcount > 1 then

repeat with aitem in inhalt

--Die Anzahl der Buchstabe einer definierten Endung... damit ich am Ende auch weiß ob ich nach

-- .tiff oder .mov suchen muss

set endcount to (-1 * (count of every character of endung))

set itemname to name of aitem

if (characters endcount through -1 of itemname) as text = endung then

--wenn es den ordner schon gibt den fehler ignorieren und weiter im Text

try

make folder at derOrdner with properties {name:ordnername}

end try

--wir basteln uns einen Alias zu einem Verzeichnis

set workingdir to ((derOrdner as string) & ordnername & ":") as alias

--tja und dieser teil bewegt die Datei in den Ordner... und bennent sie um, sofern der

--Name bereits im Zielordner existiert

-- man könnte statt des counters auch die uhrzeit bzw. das datum nehmen...

-- oh ich neheme die Uhrzeit =).

try

move aitem to workingdir as alias

on error

set no_error to false

repeat until no_error = true

set counter to (time of (current date))

try

set name of aitem to (counter & "##" & itemname) as string

move aitem to workingdir as alias

set no_error to true

exit repeat

on error

set no_error to false

end try

end repeat

end try

end if

end repeat

end if

end tell

end repeat

end adding folder items to

Safari Bookmark Saver

Man nehme den Titel und die URL der aktuellen Seite und speichere sie via Skript in einer kleinen HTML-Datei (meta-refresh)... fertig ist das Platform-übergreifende Bookmark-File

--18.01.2005

--Safari Bookmark safer

-- it's not a boookmark but a tiny html file that redirects to the saved URL

set html_text01 to "<html><meta http-equiv=\"refresh\" content=\"0; URL="

set html_text02 to "\"></html>"


tell application "Safari" to set windowcount to count of every window

if windowcount ≥ 1 then

tell application "Safari" to set theurl to URL of document 1

tell application "Safari" to set thename to name of document 1

display dialog "Filename" default answer thename

set thename to text returned of the result

set thetext to html_text01 & theurl & html_text02 as text

do shell script "echo " & quoted form of thetext & "|cat >~/Desktop/" & quoted form of thename & ".html"

end if

Folder-List

Erstelle eine Art Folder-Map... keine Ahnung wofür ich das mal brauchte...

-- 18.3.2007 hubionmac.com.com

-- creates a table of contents (tree structure) of  a folder that was dropped onto the script and saves it into a text file

global theroottxt, thefolder_unix, tabcount

on open thefolder

if (count of thefolder) > 1 then

display dialog "Please put ONE Folder onto the script"

end if

set thefolder to thefolder as alias

set thefolder_unix to POSIX path of thefolder

set theroottxt to thefolder as text

tell application "Finder"

set these_folders to every folder of thefolder

end tell

set tabcount to 0

repeat with this_folder in these_folders

set folderstring to get_folderstring(this_folder)

do shell script ("echo " & quoted form of folderstring & "|cat>> " & quoted form of thefolder_unix & "folder_list.txt") as text

do_folder(this_folder)

end repeat

end open




on do_folder(this_folder)

set tabcount to tabcount + 1

tell application "Finder"

set these_folders to every folder of this_folder

end tell

repeat with this_folder in these_folders

set folderstring to get_folderstring(this_folder)

do shell script ("echo " & quoted form of folderstring & "|cat>> " & quoted form of thefolder_unix & "folder_list.txt") as text

do_folder(this_folder)

end repeat

set tabcount to tabcount - 1

end do_folder



on get_folderstring(this_folder)

tell application "Finder"

set FolderName to name of this_folder

end tell

repeat with i from 1 to tabcount

set FolderName to tab & FolderName

end repeat

return FolderName as text

end get_folderstring

Rekursions-Beispiel

Würde ich heute ehr mit find -type d lösen, aber geht auch so

--12.10.2005 hubionmac.com

--Beispiel zum Thema Rekursion

global folderkind


set thisfolder to choose folder

display dialog "deepcount=" default answer "0"

set deepcount to text returned of the result as integer

if deepcount = 0 then

process_item(thisfolder)

else

process_folder(thisfolder, deepcount)

end if


on process_folder(this_item, deepcount)

tell application "Finder"

set these_items to every folder of this_item

set deepcount to deepcount - 1

end tell

repeat with this_item in these_items

if deepcount = 0 then

process_item(this_item)

else

process_folder(this_item, deepcount)

end if

end repeat

end process_folder


on process_item(this_item)

tell application "Finder"

set itemname to name of this_item

display dialog itemname

end tell

end process_item

No Comments

Finder: Sortieren nach “Hinzugefügt am”

10/11/2010
Ich finde die Fächer-Darstellung beim Download-Ordner recht praktisch, gerade weil man dort Dateien nach dem Datum an dem sie zum Ordner hinzugefügt wurden sortieren kann. Leider fehlt dieses Information in der normalen Finder-Ansicht und lädt man sich eine Datei via FTP oder über andere Wege, stimmt das Änderungsdatum nicht zwingend mit dem Download-Datum überein. Bei macworld.com gibt es ein Beispiel für eine Ordneraktion,
die über den Automator erstellt wird. Dort wird jede hinzugefügt Datei einfach ange-touch-t und somit das Änderungsdatum auf das aktuelle Datum geändert. Eigentlich recht elegant dachte ich, nur halt eben nur so lange bis sich das Änderungsdatum mal ändert. Aus diesem Grund habe ich mir das Kommentar-Feld ausgesucht, in dem das Datum einfach als Text eingetragen wird, so bleibt das Hinzugefügt-am-datum auch nach einer Änderung der Datei erhalten.... Bis Apple das endlich mal als Feature mit einführt.
Code zum markieren einmal anklicken Code im Skript-Editor öffnen

on adding folder items to this_folder after receiving added_items

repeat with added_item in added_items

tell application "Finder"

set timestamp to do shell script "date '+%Y-%m-%d@%H:%M:%S'"

if (comment of added_item) as text = "" then

set comment of added_item to timestamp

end if

end tell

end repeat

end adding folder items to

Kleiner Tip zur Anwendung:

Das Skript ist als Ordner-Aktion gedacht, also als normales Skript im Ordner /Library/Scripts/Folder Action Scripts/ abspeichern und als Ordner-Aktion an den Download-Ordner anhängen... fertig

No Comments

Ordneraktion: Verschiebe Datein aus Ordner A in Ordner C der in Ordner B erstellt wird…?!

16/10/2010
Ja ehm... die Überschrift trifft es ziemlich genau, mehr Worte (z.B. Ordner) würden nur verwirren =)
Code zum markieren einmal anklicken Code im Skript-Editor öffnen

--Ordner-Aktion die alle betroffenen Dateien in einen anderen (neu erstellten Ordner

-- verschiebt, dessen Name vorher abgefragt wird.

-- dieser neue Ordner wird wiederum in einem anderen Ordner erstellt, von dem sich ein Alias

-- in dem Ordner der Ordner-Aktion befindet.

-- Befindet sich bereits eine Datei mit dem gleichen Namen im Zielverzeichnis,

-- wird diese NICHT überschrieben, sondern die neue Datei wird entsprechend umbenannt (durchnummeriert).


-- ehm, um wieviele verschiedene Ordner geht es hier eigentlich?


on adding folder items to this_folder after receiving these_items

try

tell application "Finder"

set folderkind to kind of folder 1 of startup disk

set aliascount to count of every alias file of (this_folder as reference)

--wenn der Alias des Zielordners vorhanden ist kann es weitergehen

if aliascount is 1 then

set destination_folder to (original item of alias file 1 of (this_folder as reference))

set destination_folder_posix to POSIX path of (destination_folder as alias)

--ist der Zielordner auch ein Ordner oder eine Datei? Dann Fehler!

if kind of destination_folder is not folderkind then

error "Hm, der Zielordner ist gar kein Ordner..."

end if

--Ordnernamen abfragen, der im Zielordner angelegt werden soll

set foldername to text returned of (display dialog "Bitte die Ausgabe und das Jahr angeben:" buttons {"Ok"} default button "Ok" default answer "Woche-Jahr")

-- den Ordner anlegen

do shell script "mkdir -p " & quoted form of (destination_folder_posix & foldername)

-- und jetzt jede Datei in den neu angelegten Zielordner bewegen, dabei aber auf doppelte Dateinamen achten und ggf. umbenennen

repeat with this_item in these_items

set item_name to name of this_item

set this_item_posix to POSIX path of (this_item as reference)

set uniq_name to my checkname_with_pdf_suffix(item_name, folder foldername of destination_folder, false)

do shell script "mv " & quoted form of this_item_posix & " " & quoted form of (destination_folder_posix & foldername & "/" & uniq_name)

end repeat

else

error "Zielalias nicht vorhanden oder es gibt mehr als einen Alias..."

end if

end tell

on error msg

display dialog msg

end try

end adding folder items to



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

No Comments

PDF Wasserzeichen die II.

27/04/2008
Ich hatte ja bereits vor einiger Zeit mal eine Anleitung geschrieben, wie man PDFs mit Wasserzeichen versehen kann. Damals über einen CUPS-PDF-Drucker, der bei Belieben das PDF auch gleich weiter verschickt. Das ist sicherlich sehr praktisch für einen Server, nur habe ich jetzt die Client-Variente fertig =) Dabei handelt es sich um einen Ordner-Aktion (10.4-10.5), die jedes PDF, das in den Ordner gelegt wird, mit einem Wasserzeichen versieht und dann an eine neue Email in AppleMail anhängt oder das PDF einfach nur öffnen. Das besondere daran, ist dass ich es
endlich geschafft habe die Software, die das überhaupt erst möglich macht - pdftk nämlich - so zu kompilieren, dass sie wohl auf jedem Mac ob PPC oder INTEL unter 10.4 und 10.5 läuft. Im Klartext heisst das, dass sie auf PPCs normal läuft und auf Intel-Rechnern unter Rosetta, was der Geschwindigkeit in diesem Falle aber nicht wirklich abträglich ist. Ich habe einen Installer via AppleScript geschrieben, der alles notwendige installiert, alles was man dann noch machen muss, sind die Aktivierung der Ordneraktionen und das Anhängen eines der Skripte an einen Odner.
Die Handhabung ist also wirklich simpel, was man vielleicht noch selber anpassen sollte, wäre das Hintergrund PDF. Dieses sollte als Hintergrund auch nicht deckend weiss sein, sondern transparent, da es quasi auf die PDFs "gestempelt" wird. So ist man wenigsten sicher, dass das Firmen-Logo nie verdeckt wird =) (ich habe aber auch eine Version der Skripte mit dazu gepackt,
die das PDF als Hintergrund auftragen). Noch eine Info zu den erstellten PDFs, diese werden von pdftk verschlüsselt und komprimiert gespeichert, d.h. man darf sie danach öffnen und drucken, nicht aber verändern! Ich habe in den Skripten einen festes Kennwort hinterlegt. Nun, hier der Download und über Feedback, ob es auch bei euch funktioniert, würde ich mich sehr freuen!

Hubi's PDF-Watermarker

DOWNLOAD
Hubi's PDF-Watermarker v.1.0b
(648 hits)

Nachtrag:

Ich habe noch eine kleine Library unterschlage, die man noch benötigt, um pdftk korrekt ausführen zu können. Das Image ist aktualisiert und nun sollte es wohl auch keine Probleme mehr geben.
13 Comments

CDs/DVDs automatisch nach dem Einlegen kopieren und auswerfen

28/01/2008
Ich sichere gerade meine gesammelten DVDs/CDs auf meinem NAS und das sind so ca. 200.Alle von Hand zu kopieren ist nicht nur nervig, sonder kostet auch viel Zeit, da ich nicht immer gleich mitbekommen, wenn ein Kopiervorgang fertig ist.Deshalb habe ich eine Ordner-Aktion geschrieben, die ich an den unsichtbaren "Volumes" Ordner angehängt habe. Sobald nun ein neues Medium eingelegt wird, wird dieses nach einem kurzen Dialog mit Timeout auf das NAS kopiert und anschließend ausgeworfen.Hier zunächst einmal der Code:  
Code zum markieren einmal anklicken

--Folder action to copy contents of inserted media to some other place...

on adding folder items to this_folder after receiving these_items

try

tell application "Finder"

set b to POSIX path of ((original item of alias file "COPYALIAS" of startup disk) as alias)

end tell

repeat with this_ in these_items

tell application "Finder"

display dialog ("copy " & (name of this_) & "?") as text giving up after 3

set itemname to name of this_

set copypath to (original item of alias file "COPYALIAS" of startup disk)

set a to POSIX path of this_

--set a to (characters 1 through -2 of a) as text

end tell

set bb to checkname_now_suffix(itemname, copypath, false)

do shell script "cp -R " & quoted form of a & " " & quoted form of b & quoted form of bb

tell application "Finder"

eject this_

end tell

end repeat

on error msg

tell application "Finder"

activate

display dialog msg

end tell

end try

end adding folder items to


on checkname_now_suffix(n, D, looped)

tell application "Finder"

set thefiles to name of every item of D

end tell

if thefiles contains n then

if looped = false then

set n to n & " 1"

else

set cc to count of every character of last word of n

set cn to ((last word of n) as integer) + 1

set n to ((characters 1 through (-1 * (cc + 1)) of n) as text) & cn

checkname_now_suffix(n, D, counter, true)

end if

else

return n

end if

end checkname_now_suffix


Man zieht einen Alias von dem Zielverzeichnis direkt auf das Start-Volumen und nennt ihn "COPYALIAS" so weiss das Skript, wohin kopiert werden soll.
No Comments