Archive of articles classified as' "AppleScript"

Back home

Dateiliste in Numbers führen an Hand von Dateinamen

14/09/2011
Ich habe z.B. in Numbers eine Liste mit allen Job-IDs und möchte in einer Spalte daneben alle Dateien aus einem Bestimmten Verzeichnis auflisten, deren Name diese Job-ID beinhaltet... nix leichter als das:

--14.09.2011 hubionmac.com

-- parts of numbers selection code by http://hints.macworld.com/article.php?story=20090109055630154

-- requested @https://discussions.apple.com/thread/3325583?tstart=0

-- this script takes the value of a selection in numbers first and a asks for a folder

-- then the values of the first column of the selection are taken and all filenames of start start with that folder and are found in the defined folder will be

-- writen to the last column of the selection ----> you will have to select at least 2 columns


set thefolder to quoted form of POSIX path of ((choose folder) as alias)


tell application "Numbers"

activate

tell document 1

-- DETERMINE THE CURRENT SHEET

set current_sheet_index to 0

repeat with i from 1 to the count of sheets

tell sheet i

set x to the count of (tables whose selection range is not missing value)

end tell

if x is not 0 then

set the current_sheet_index to i

exit repeat

end if

end repeat

if the current_sheet_index is 0 then error "No sheet has a selected table."

-- GET THE VALUES OF THE SELECTED CELLS

tell sheet current_sheet_index

set the current_table to the first table whose selection range is not missing value

tell the current_table

set range_column_count to count of every column of selection range

set this_rows_first_cell_index to 1

set this_rows_last_cell_index to range_column_count

-- THIS CHANGES THE VALUE OF 

repeat with i from 2 to count of every cell of selection range

set thename to value of cell this_rows_first_cell_index of selection range

set foundfilenames to my find_filenames(thefolder, thename)

set value of cell this_rows_last_cell_index of selection range to foundfilenames

set this_rows_first_cell_index to this_rows_first_cell_index + range_column_count

set this_rows_last_cell_index to this_rows_last_cell_index + range_column_count

if this_rows_last_cell_index > (count of every cell of selection range) then exit repeat

end repeat

end tell

end tell

end tell

end tell



on find_filenames(thefolder, filename_start)

-- THIS SEARCHES FILES STARTING WITH A SPECIFIC STRING IN THEIR NAMES AND OUTPUTS JUST THEIR FILENAMES

-- IS SEARCHES IN THEFOLDER AND ALL SUB-DIRECTORIES

set theoutput to do shell script "cd " & thefolder & ";find " & "." & " -iname \"" & filename_start & "*\""

if theoutput ≠ "" then

set theoutput to every paragraph of theoutput

--OPTIONAL REMOVE THE FIRST CHARACTER OF EACH PATH

set tmp to {}

repeat with t in theoutput

set tmp to tmp & {(characters 2 through -1 of t) as text}

end repeat

set theoutput to tmp

set AppleScript's text item delimiters to "; "

set theoutput to theoutput as text

set AppleScript's text item delimiters to ""

end if

return theoutput

end find_filenames

No Comments

iCal: Arbeitszeiterfassung die 2.

12/08/2011

Marko hat mich gebeten das Zeiterfassungs-Skript, dass ich mal vor einiger Zeit geschrieben habe, etwas zu erweitern, so dass auch einen Statistik für den gesamten Monat ausgegeben wird. Zudem ist in diese Version auch ein Growl-Status-Anzeige eingebaut, so dass das Skript nicht kommentarlos den Kalender befüllt. Ich habe das Skript mit 10.7 getestet, ich denke es sollte aber auch problemlos noch unter den letzten beiden System funktionieren (Die App müsste eine reine Intel App sein, da 10.7 nicht anderes mehr auszugeben versteht. Auf einem PPC müsste der AppleScript-Editor aber damit zurande kommen, also in dem Fall einfach neu als App speichern und man hat wieder eine CarbonApp) =)DOWNLOAD
iCal_Time-Recording v.1.2
191.69 kB (98 hits)

7 Comments

OS X Lion: Fensterwiederherstellung für einzelne Programme sperren

11/08/2011
defaults write com.apple.QuickTimePlayerX NSQuitAlwaysKeepsWindows -bool false via OS X Lion: Fensterwiederherstellung für einzelne Programme sperren « macnews.de.In der Library, unter Saved Application State werden alle Programme aufgeführt, bei denen ein solcher SavedState bereits gespeichert wurde =) also warum nicht ein kleines Skript:

--11.08.2011 little script to edit saved state settings for a single app instead of all apps

global myhome

tell application "Finder"

set myhome to POSIX path of (home as alias)

set theitems to name of every item of folder "Saved Application State" of folder "Library" of home

set theapps to {}

repeat with theitem in theitems

if (theitem as text) ends with ".savedState" then

set theapps to theapps & ((characters 1 through -12 of theitem as text) as text)

end if

end repeat

end tell

set theapp to (choose from list theapps with prompt "Which app with saved state?") as text

set theactions to {"Delete SavedState", "Disable SavedState", "Enable SavedState", "Freeze Saved State", "Defrost Saved State"}

set theaction to (choose from list theactions with prompt "Which action?") as text

if theaction = (item 1 of theactions) as text then

my deleteSavedState(theapp)

else if theaction as text = (item 2 of theactions) as text then

my disableSavedState(theapp)

else if theaction as text = (item 3 of theactions) as text then

my enableSavedState(theapp)

else if theaction as text = (item 4 of theactions) as text then

my freezeSavedState(theapp)

else if theaction as text = (item 5 of theactions) as text then

my defrostSavedState(theapp)

end if


on deleteSavedState(appName)

--moves avedState to user's trash folder and plays drag to trash sound

do shell script "mv " & myhome & "Library/'Saved Application State'/" & quoted form of appName & ".savedState ~/.Trash/;afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/dock/drag\\ to\\ trash.aif"

end deleteSavedState


on disableSavedState(appName)

--uses defaults write to disable saved state for this particular app

do shell script "defaults write " & appName & " NSQuitAlwaysKeepsWindows -bool false"

do shell script "afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/ink/InkSoundBecomeMouse.aif"

end disableSavedState

on enableSavedState(appName)

--uses defaults write to disable saved state for this particular app

do shell script "defaults write " & appName & " NSQuitAlwaysKeepsWindows -bool true"

do shell script "afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/ink/InkSoundBecomeMouse.aif"

end enableSavedState


on freezeSavedState(appName)

--changes user permissions to read only, so that savedStated is frozen

do shell script "chmod u-w " & myhome & "Library/'Saved Application State'/" & quoted form of appName & ".savedState"

do shell script "afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/ink/InkSoundBecomeMouse.aif"

end freezeSavedState

on defrostSavedState(appName)

--changes user permissions to read&write again, so that savedStated is not frozen any more

do shell script "chmod u+w " & myhome & "Library/'Saved Application State'/" & quoted form of appName & ".savedState"

do shell script "afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/ink/InkSoundBecomeMouse.aif"

end defrostSavedState

Update:

Ok, das hier ist nun wirklich eine schönere Lösung: http://www.tuaw.com/2011/07/26/dear-aunt-tuaw-help-me-fine-tune-session-window-restores/
No Comments

Umlaute und Sonderzeichen in Datei- und Ordnernamen ersetzen

2/08/2011
Das war der Auslöser und es hier meine Lösung dazu:

-- hubionmac.com 02.08.2011

-- ersetzt in einem Verzeichnis und allen Unterordnern Sonderzeichen (Umlaute) in Datei und Ordnernamen

-- es können so nur einzelne Sonderzeichen gegen Zeichenketten ersetzt werden ä ->ae

set replacements_list to {{"Ä", "Ae"}, {"ä", "ae"}, {"Ö", "Oe"}, {"ö", "oe"}, {"Ü", "ue"}, {"ü", "ue"}, {"/", "_"}}


set thefolder to choose folder

set thefiles_x to do shell script "find " & quoted form of (POSIX path of thefolder) & " -not -name \".*\""

-- Baut sich eine Liste aus Aliasen, damit die Pfadangaben auch noch funktionieren,

--wenn mal ein übergeordnetes Verzeichnis bereits vom Skript umbenannt wurde

set thefiles to {}

repeat with thefile in every paragraph of thefiles_x

set thefiles to thefiles & ((POSIX file thefile) as alias)

end repeat


repeat with thefile in thefiles

tell application "Finder"

set thefilename to name of thefile

set old_filename to thefilename

end tell

repeat with replacement in replacements_list

set badchar to ASCII number of ((item 1 of replacement) as text)

set thefilename_ascii to asciilist(thefilename)

if badchar is in thefilename_ascii then

set thefilename to my replace_string(thefilename, ASCII character badchar, (item 2 of replacement) as text)

end if

end repeat

if old_filenamethefilename then

tell application "Finder"

set name of thefile to thefilename

end tell

end if

end repeat


on replace_string(itemname, searchstring, replacestring)

set old_delimiter to AppleScript's text item delimiters

set AppleScript's text item delimiters to searchstring

set the item_list to every text item of itemname

set AppleScript's text item delimiters to replacestring

set this_text to the item_list as string

set AppleScript's text item delimiters to old_delimiter

return this_text

end replace_string


on asciilist(thestring)

set myoutput to {}

repeat with a in every character of thestring

set myoutput to myoutput & (ASCII number of a)

end repeat

return myoutput

end asciilist

1 Comment

Adressbuch: checkt ob eine Telefonnummer bereits vorhanden ist

30/06/2011
Das ist nur als Beispiel gedacht, wie so eine Funktion aussehen könnte:

tell application "Address Book"

activate

set my_phone_2_check to text returned of (display dialog "Telefonnummer:" default answer "0")

set allp to people

set phone_numbers to value of every phone of people

repeat with i from 1 to count of phone_numbers

set current_phone_numbers to item i of phone_numbers

if (count of current_phone_numbers) > 0 then

repeat with phone_number_2_check in current_phone_numbers

set phone_number_2_check to phone_number_2_check as text

if phone_number_2_check starts with "+" then

set phone_number_2_check to ("0" & characters 4 through -1 of phone_number_2_check) as text

end if

if phone_number_2_check contains " " then

set phone_number_2_check to my replace_chars(phone_number_2_check, " ", "")

end if

if phone_number_2_check contains "-" then

set phone_number_2_check to my replace_chars(phone_number_2_check, "-", "")

end if

if phone_number_2_check contains "/" then

set phone_number_2_check to my replace_chars(phone_number_2_check, "/", "")

end if

if phone_number_2_check = my_phone_2_check then

set selection to item i of people

error "Die Nummer gibt es schon"

end if

end repeat

end if

end repeat

end tell

to replace_chars(this_text, search_string, replacement_string)

try

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

on error msg

error "error on replace_chars" & return & msg

end try

end replace_chars

No Comments

ChronoSync: Nerven bis die Platte dran ist…

24/05/2011

ChronoSync scheint ein sehr schönes Backup-Programm zu sein, schafft es aber nicht nachzufragen, ob man nicht auch mal die passende Festplatte für das Backup anschließen möchte. Stattdessen schlägt das Backup direkt fehl und man muss die Platte anschließend mounten und das Backup von Hand wieder anstoßen. Laut Support kann man sich eine entsprechende Abfrage per AppleSkript selber einbauen... Elgato fährt meiner Erinnerung nach die selbe Schiene: "Wenn Ihnen dieses (zugegebenermaßen grundlegende) Feature so wichtig sein sollte, können sie diese Funktion gerne selber per Skript einbinden." So lautete vor einigen Jahren die Antwort auf die Frage ob die Software den Rechner nicht auch ein/ausschalten könne. Aber da möchte ich gar nicht so negativ über diese Haltung herziehen, schließlich ist AppleSkript ja dafür da, dem User die Möglichkeit zu geben, eine Sofware um praktische Funktionen zu erweitern. Und AppleSkript-Support ist für eine Anwendung unter 40$ ja auch nicht gerade selbstverständlich. Um so besser, wenn es auch dem Naturell des Benutzer entspricht, den Feinschliff der gekauften Software selbst zu übernehmen ;-P

-- 24.05.2011 hubionmac.com

-- can be added as as a pref-sync script to a  ChronoSync-Job to ask the user

-- to connect a missing source/destination volume if needed

--https://discussions.apple.com/thread/3075920


tell application "ChronoSync"

set mysource to leftTargetPath of document 1

set mydesti to rightTargetPath of document 1

if mysource starts with "/Volumes/" then

set mydelimiter to AppleScript's text item delimiters

set AppleScript's text item delimiters to "/"

set sourceName to text item 3 of mysource

set AppleScript's text item delimiters to mydelimiter

else

tell application "Finder" to set sourceName to name of startup disk

end if

if mydesti starts with "/Volumes/" then

set mydelimiter to AppleScript's text item delimiters

set AppleScript's text item delimiters to "/"

set destiName to text item 3 of mydesti

set AppleScript's text item delimiters to mydelimiter

else

tell application "Finder" to set destiName to name of startup disk

end if

tell application "Finder" to set drivenames to name of every disk

set cancelbackup to false

if sourceName is not in drivenames then

repeat until 1 = 0

set myaction to button returned of (display dialog "Source disk (" & sourceName & ") is not mountet!" buttons {"Check again!", "Cancel Backup"} default button 1 giving up after 30)

if myaction = "Check again!" then

tell application "Finder" to set drivenames to name of every disk

if sourceName is in drivenames then

exit repeat

end if

else

set cancelbackup to true

exit repeat

end if

end repeat

end if

if cancelbackup is false then

if destiName is not in drivenames then

repeat until 1 = 0

set myaction to button returned of (display dialog "Destination disk (" & destiName & ") is not mountet!" buttons {"Check again!", "Cancel Backup"} default button 1 giving up after 30)

if myaction = "Check again!" then

tell application "Finder" to set drivenames to name of every disk

if destiName is in drivenames then

exit repeat

end if

else

set cancelbackup to true

exit repeat

end if

end repeat

end if

end if

if cancelbackup is true then error "Canceling"

end tell

No Comments

Wie lange braucht mein Mac zum Aufwachen…

7/05/2011
Ich finde es etwas fragwürdig, das per Skript zu messen... aber wie sonst? Es ist aber ein nettes Beispiel, wie man pmset skripten kann.

--07.05.2011 hubionmac.com

--skript that measures the time until wakeup is complete

-- this gives a f*** about your wakeorpoweron settings, they will be deleted!!!


set mystartup to build_pmset_date(1)

set ct to current date

tell application "Finder" to sleep

repeat until 1 = 0

if ct > mystartup then

exit repeat

else

set ct to current date

delay 1

end if

end repeat

set diff to ct - mystartup

display dialog "Waking up took me " & diff & " sec."

--clean wakup-plan

do shell script "pmset repeat cancel" with administrator privileges



on build_pmset_date(plusminutes)

set d to (current date) + (plusminutes * 60)

--make sure that we will end up with at least 1 minute...

if seconds of d > 30 then

set d to d + 60

end if

set thehour to characters -2 through -1 of (("0" & (hours of d as integer)) as text)

set theminutes to characters -2 through -1 of (("0" & (minutes of d as integer)) as text)

--pmset does not care about seconds...

set theseconds to "00"

set seconds of d to 0

set thetime to thehour & ":" & theminutes & ":" & theseconds

--clean wakup-plan

do shell script "pmset repeat cancel" with administrator privileges

--set the plan

do shell script "pmset repeat wakeorpoweron MTWRFSU \"" & thetime & "\"" with administrator privileges

return d

end build_pmset_date

No Comments

ISBN Nummern aus PDFs auslesen

30/04/2011
Ist eigentlich nur ein Beispiel, wie man recht ohne großen Overhead (gs mal vorausgesetzt) PDFs auslesen kann... vielleicht kennt ja jemand noch ein simplere Lösung:

--30.04.2011 hubionmac.com

--quick & dirty script to extract first ISBN-Number of a PDF file... just uses grep, maybe some nice reqexp would do a better job!


set myselection to choose file of type {"pdf"} with multiple selections allowed

set myoutput to ""

repeat with pdf_file in myselection

tell application "Finder" to set pdfname to name of (pdf_file as alias)

set pdf_file_posix to quoted form of POSIX path of (pdf_file as alias)

do shell script ""

try

--first add the path, otherwhise s2ascii will fail since it cannot find ghostscript (gs) which is also installed in /usr/local/bin (think by macports)

set ISBN_String to do shell script "PATH=\"$PATH:/usr/local/bin\"; /usr/local/bin/ps2ascii " & pdf_file_posix & " | grep -m 1 ISBN"

set foundLine to true

on error

display dialog "maybe \"" & pdfname & "\" does not contain a ISBN at all"

set foundLine to false

end try

if foundLine is true then

repeat with s in every word of ISBN_String

try

get s as integer

set s to s as text

set foundisbn to true

exit repeat

on error

set s to ""

end try

end repeat

end if

set myoutput to myoutput & pdfname & tab & s & return

end repeat


tell application "TextEdit"

activate

set a to make new document

set text of a to myoutput as text

end tell

No Comments

Photoshop: Text aus Zwischenablage in aktuelles Layer einfügen

26/03/2011
Ohne großen Aufwand den Text eines Text-Layers in Photoshop zu ersetzen... einfach den Text in die Zwischenablage kopieren, das gewünschte Text-Layer auswählen und das Skript (am besten per Shortcut) starten:

tell application "Adobe Photoshop Elements"

try

set contents of text object of current layer of current document to (the clipboard as text)

on error msg

beep

end try

end tell

No Comments

GUI Scripting: Sondertasten umstellen…

25/03/2011
Ich habe immer noch nicht viel für GUI-Scripting übrig, aber wie sonst könnte man die Sondertasten per Skript umstellen?

-- hubionmac.com 24.03.2011 (tested with 10.6.7)


tell application "System Preferences"

activate

set current pane to pane "com.apple.preference.keyboard"

tell application "System Events"

tell process "System Preferences"

click button 1 of tab group 1 of window 1

set curOpt to (get value of pop up button 3 of sheet 1 of window 1)

set curCmd to (get value of pop up button 4 of sheet 1 of window 1)

click pop up button 4 of sheet 1 of window 1

my set_button_item(1)

click pop up button 3 of sheet 1 of window 1

my set_button_item(2)

click pop up button 2 of sheet 1 of window 1

my set_button_item(3)

click pop up button 1 of sheet 1 of window 1

my set_button_item(4)

delay 0.25

keystroke return

end tell

end tell

quit

end tell


on set_button_item(valueindex)

tell application "System Events"

tell process "System Preferences"

repeat with i from 1 to 5

key code 126

delay 0.25

end repeat

repeat with i from 1 to valueindex - 1

key code 125

delay 0.25

end repeat

keystroke return

end tell

end tell

end set_button_item

No Comments