AppleScript: Monitore synchronisieren aktivieren per Skript

19/03/2010
Ist zwar übelstes GUI-Skripting, mag aber dem ein oder anderen helfen...
Code zum markieren einmal anklicken Code im Skript-Editor öffnen

do shell script "open /System/Library/PreferencePanes/Displays.prefPane"

delay 2 --wenn Dein Rechner da langsamer ist... größerer Delay... I love GUI-Scripting

activate application "System Preferences"

tell application "System Events"

tell process "System Preferences"

if title of radio button 2 of tab group 1 of window 1 = "Anordnen" then

click radio button "Anordnen" of tab group 1 of window 1

click checkbox 1 of group 1 of tab group 1 of window 1

else

display dialog "Falsche Sprache oder kein 2. Monitor"

end if

end tell

end tell

tell application "System Preferences" to quit

Übrigens ein geniales Tool für GUI-Skripting ist der UI-Browser, wenngleich ich die 55$ für ein solches Tool einfach nur überzogen finde!
No Comments

iTunes: Download Order mit iTunes abgleichen (mit playlists)

10/10/2009
Das ging eigentlich schon etwas früher mit meinen iTunes-Scripts (04-scan folder and add to iTunes Lib), hinzugekommen ist aber das Anlegen der einzelnen Playlisten.
Ist recht praktisch, um einen Überblick über alle Medien-Datein zu bekommen, die so in den Downloads dümpeln... Coole Idee von Rounak
Code zum markieren einmal anklicken Code im Skript-Editor öffnen

-- hubionmac.com 10.10.2009

-- scans download folder for folders containing media data iTunes can play

-- creates a corresponding playlist within itunes

property special_bigChars : {"Ä", "Å", "Ç", "É", "Ñ", "Ö", "Ü", "À", "Ã", "Õ", "Ÿ", "Â", "Ê", "Á", "Ë", "È", "Í", "Î", "Ï", "Ì", "Ó", "Ô", "Ò", "Ú", "Û", "Ù"}

property special_smallChars : {"ä", "å", "ç", "é", "ñ", "ö", "ü", "à", "ã", "õ", "ÿ", "â", "ê", "á", "ë", "è", "í", "î", "ï", "ì", "ó", "ô", "ò", "ú", "û", "ù"}

property action_list02 : {"01-Abcd Efg", "02-Abcd efg", "03-ABCD EFG", "04-abcd efg"}


global folder2Scan, actionid



tell application "Finder"

set actionid to (my get_selection_index(action_list02, "Format of Playlist_names (cancel for no changes)", false))

set folder2Scan to folder "Downloads" of home

set thefolders to every container of folder2Scan

set thefiles to every file of folder2Scan

--get my download folder

set thefolders_list to my makeDownloadPlaylist()

--clean the folderlist from old playlist // option dialog would be nice

my clean_iTunes_folder(thefolders_list)

-- make a folder playlist called itunes or get reference to it

tell application "iTunes"

set files_in_folder2Scan to my check_playlist(thefolders_list, "•SINGLE FILES•")

repeat with thefile in thefiles

if name of thefile does not end with ".pdf" then

add (thefile as alias) to files_in_folder2Scan

end if

end repeat

if (count of every track of files_in_folder2Scan) = 0 then

delete files_in_folder2Scan

else

--clean the playlist from duplicates

my cleanPlaylist(files_in_folder2Scan)

end if

repeat with thefolder in thefolders

set foldername to my do_string(actionid, my replace_chars((name of thefolder), "_", " "))

--now the script loops through every folder, creates a corrensponding playlist

-- if there created playlist is empty after all, it will be removed again =)

set current_playlist to my check_playlist(thefolders_list, foldername)

add (thefolder as alias) to current_playlist

if (count of every track of current_playlist) = 0 then

delete current_playlist

else

--clean the playlist from duplicates

my cleanPlaylist(current_playlist)

end if

end repeat

end tell

end tell


display dialog "done...

***commerical***

created by hubionmac.com :-P

***commerical***"



on check_playlist(folderplaylist, playlistname2check)

tell application "iTunes"

set thelists to every user playlist of source 1 whose special kind is none and smart is false

set fpl_lists to {}

repeat with i in thelists

try

if parent of i = folderplaylist then

set fpl_lists to fpl_lists & {i}

end if

end try

end repeat

repeat with i in fpl_lists

if (name of i) as text = playlistname2check then

return i

end if

end repeat

make new playlist with properties {name:playlistname2check} at folderplaylist

end tell

end check_playlist


on makeDownloadPlaylist()

tell application "iTunes"

-- set thelists to name of every user playlist of source 1 whose special kind is none and smart is false

if (count of every folder playlist) = 0 or (name of every folder playlist) does not contain "Downloads" then

tell source 1 to make new folder playlist with properties {name:"Downloads"}

set fpl to folder playlist "Downloads" of source 1

else

set fpl to folder playlist "Downloads" of source 1

end if

return fpl

end tell

end makeDownloadPlaylist


on cleanPlaylist(theplaylist)

tell application "iTunes"

set idlist to {}

repeat with i from the (count of tracks) of theplaylist to 1 by -1

set currentID to database ID of track i of theplaylist

if currentID is in the idlist then

delete track i of theplaylist

else

if ((location of track i of theplaylist) as text) starts with folder2Scan then

set idlist to idlist & {currentID}

else

delete track i of theplaylist

end if

end if

end repeat

end tell

end cleanPlaylist



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 do_string(actionid, this_text)

end replace_chars




on do_string(actionid, the_string)

if actionid = {} then

return the_string

else

set actionid to actionid as integer

set finalstring to ""

if actionid = 1 then

repeat with i from 1 to count of characters of the_string

set test_char to character i of the_string

if i = 1 then

set finalstring to makebig(test_char)

else

set prev_char to character (i - 1) of the_string --last character of finalstring as text

if followed_by_bigchar(prev_char) = true then

set finalstring to finalstring & makebig(test_char)

else

set finalstring to finalstring & makesmall(test_char)

end if

end if

end repeat

else if actionid = 2 then

set firstchar to makebig(character 1 of the_string)

repeat with i from 2 to count of characters of the_string

set test_char to character i of the_string

set finalstring to finalstring & makesmall(test_char)

end repeat

set finalstring to (firstchar & finalstring) as text

else if actionid = 3 then

repeat with i from 1 to count of characters of the_string

set test_char to character i of the_string

set finalstring to finalstring & makebig(test_char)

end repeat

else if actionid = 4 then

repeat with i from 1 to count of characters of the_string

set test_char to character i of the_string

set finalstring to finalstring & makesmall(test_char)

end repeat

end if

return finalstring

end if

end do_string




on followed_by_bigchar(test_char)

if ((ASCII number of test_char) is greater than 64) and ¬

((ASCII number of test_char) is less than 91) then

return false

else if ((ASCII number of test_char) is greater than 96) and ¬

((ASCII number of test_char) is less than 123) then

return false

else if special_bigChars contains test_char then

return false

else if test_char = "'" then

return false

else

return true

end if

end followed_by_bigchar



on makesmall(test_char)

--when it's a normal character

if ((ASCII number of test_char) is greater than 64) and ¬

((ASCII number of test_char) is less than 91) then

return (ASCII character ((ASCII number of test_char) + 32))

--when it's a special character

else if test_char is in special_bigChars then

repeat with i from 1 to count of special_bigChars

if item i of special_bigChars = test_char then

return item i of special_smallChars

end if

end repeat

--when it's something else

else

return test_char

end if

end makesmall


on makebig(test_char)

--when it's a normal character

if ((ASCII number of test_char) is greater than 96) and ¬

((ASCII number of test_char) is less than 123) then

return (ASCII character ((ASCII number of test_char) - 32))

--when it's a special character

else if test_char is in special_smallChars then

repeat with i from 1 to count of special_bigChars

if item i of special_smallChars = test_char then

return item i of special_bigChars

end if

end repeat

--when it's something else

else

return test_char

end if

end makebig


on get_selection_index(action_list, theprompt, mult_selection)

set theselection to choose from list action_list multiple selections allowed mult_selection with prompt theprompt

set returnlist to {}

repeat with theselected in theselection

set i to 1

repeat with theaction in action_list

if theselected as text = theaction as text then

set returnlist to returnlist & i as list

end if

set i to i + 1

end repeat

end repeat

return returnlist

end get_selection_index


on clean_iTunes_folder(folder_playlist_2_clean)

tell application "iTunes"

set thelists to every user playlist of source 1 whose special kind is none and smart is false

repeat with i in thelists

try

if parent of i = folder_playlist_2_clean then

delete i

end if

end try

end repeat

end tell

end clean_iTunes_folder

2 Comments

Rsync mit AppleScript, Ordner Synchronisieren bzw. Backup erstellen

1/02/2009
Die Idee ist eigentlich voll simpel uns sicher schon irgendwie mal programmiert worden. Das nette an dieser Lösung ist, dass die Ziel und Quell-Ordner über Aliase gekennzeichnet werden.Man zieht einmal den zu sichernden Ordner als
Alias in den Skript-Ordner, dann den ZielOrdner und bennent diese entsprechen in um (SOURCE und DESTINATION). Beim nächsten Start, synchronisiert das Skript mit rsync und schreibt alles in ein Log, das 2000 Zeilen nie übersteigt...
Code zum markieren einmal anklicken

try

set sourcepath to ""

set destipath to ""

set h to path to me

tell application "Finder"

set h to container of h

if (count of every alias file of h) = 2 then

set thealiases to every alias file of h

repeat with thealias in thealiases

if name of thealias = "SOURCE" then

set sourcepath to characters 1 through -2 of (POSIX path of (original item of thealias as alias)) as text

else if name of thealias = "DESTINATION" then

set destipath to POSIX path of (original item of thealias as alias)

end if

end repeat

if destipath "" and sourcepath "" then

do shell script "rsync -avE --delete-after " & quoted form of sourcepath & " " & quoted form of destipath & " >> " & quoted form of POSIX path of (h as alias) & "rsync_log.txt || echo -n"

do shell script "tail -n 2000 " & quoted form of POSIX path of (h as alias) & "rsync_log.txt | cat> " & quoted form of POSIX path of (h as alias) & "rsync_log.txt2; mv " & quoted form of POSIX path of (h as alias) & "rsync_log.txt2 " & quoted form of POSIX path of (h as alias) & "rsync_log.txt"

else

error "irgendwas ist da falsch gelaufen"

end if

end if

end tell

on error msg

error "irgendwas ist da falsch gelaufen" & msg

end try

No Comments

Ordner via AppleScript synchronisieren über md5 Prüfsumme

5/10/2008
Hatte mal wieder keine Lust ins Bett zu gehen und dabei ist dann das hier heraus gekommen:

(**

Installation:


   1. Wenn Du es das erste mal startest, erzeugt es einen Ordner mit dem Namen "SyncFolders_Script_aliases" auf deinem Start-Volumen und öffnet diesen.

   2. In diesen Ordner ziehst Du zunächst Aliase der Quell-Ordner (ja Du kannst mehrere Ordner in einen Zielordner synchronisieren).

   3. Diese Ordner-Alias bennenst Du um in SOURCE.. irgendwas, wichtig ist nur, dass SOURCE am Anfang steht

   4. Dann erstellst Du da noch einen Alias deines Zielordners, den Du DESTINATION irgendwas nennst



Dann nur noch starten und das Skript legt los =)



Wie es funktioniert?:

Die Dateien werden an Hand von md5 Prüfsummen unterschieden. Jede Dateien, deren md5-Prüfsumme mit denen der Dateien im Zielordner nicht übereinstimmt, wird umbenannt, nach dem Schema (+OUT<aktuelles Datum>) und dann kopiert. Sollte es beim Kopieren zu Problemen kommen, wird die Datei wieder "zurück-benannt" und das Skript läuft weiter. Am Ende erscheint in dem Fall aber eine Fehlermeldung.

In dem Skript Ordner wird zudem ein Datei log.txt geführt, in der dann bei jedem Start geschrieben wird, wie viel wohin kopiert wurde und ob es Fehler gegeben hat...

**)

set script_folder to "SyncFolders_Script_aliases"

set copycount to 0

set copiedpaths to ""

set errorpaths to ""

checkup(script_folder)


tell application "Finder"

set alias_folder to folder script_folder of startup disk

set source_aliases to (every alias file of alias_folder whose name starts with "SOURCE")

set destination_alias to (every alias file of alias_folder whose name starts with "DESTINATION")

end tell

set destination_files to md5_list(item 1 of destination_alias)

set source_files to {}

repeat with i from 1 to count of source_aliases

set source_files to source_files & md5_list(item i of source_aliases)

end repeat


-- assume that all sources are new sources

set newsources to source_files


repeat with destination_file in destination_files

set b to md5 of destination_file

repeat with source_file in source_files

if md5 of source_file = b then

--finally only new sources will stay in the newsources list 

set newsources to delete_from_list(source_file, newsources)

end if

end repeat

end repeat

set erroronmove to false

set prestring to "OUT+" & (do shell script "date +%Y%m%d")

repeat with newsource in newsources

set current_file to MacOSPath of newsource

tell application "Finder" to set current_file_name to (name of current_file)

--If the would be files already named "OUT+..:" that are NOT in the destination path 

--these would no be copied 

--if current_file_name does not start with "OUT+" then

set current_file to rename_file(current_file, prestring, current_file_name)

try

tell application "Finder"

set copiedfile to duplicate current_file to (original item of (item 1 of destination_alias)) as alias

set copycount to copycount + 1

set copiedpaths to copiedpaths & (POSIX path of (copiedfile as alias) as text) & return

end tell

on error msg

rename_file(current_file, "", current_file_name)

set errorpaths to errorpaths & (POSIX path of (current_file as alias) as text) & return

set erroronmove to true

end try

--end if

end repeat



--write Logfiles

set msg to "Copied " & copycount & " files at "

do shell script "echo " & quoted form of msg & "|cat>>" & quoted form of (POSIX path of (alias_folder as alias)) & "log.txt"

do shell script "date | cat>>" & quoted form of (POSIX path of (alias_folder as alias)) & "log.txt"

if copiedpaths "" then

do shell script "echo " & quoted form of copiedpaths & "|cat>>" & quoted form of (POSIX path of (alias_folder as alias)) & "log.txt"

end if

if erroronmove = true then

set msg to "Errors with files: "

do shell script "echo " & quoted form of msg & "|cat>>" & quoted form of (POSIX path of (alias_folder as alias)) & "log.txt"

do shell script "echo " & quoted form of errorpaths & "|cat>>" & quoted form of (POSIX path of (alias_folder as alias)) & "log.txt"

display dialog "Some files could not be copied (name already exsist perhaps) and so presstring has been removed from there names again (just in case the prestring is :" & return & prestring giving up after 10

end if



on rename_file(thefile, new_start, thefile_name)

tell application "Finder"

set no_error to false

set counter to 0

try

set name of thefile to new_start & thefile_name

on error

repeat until no_error = true

set counter to counter + 1

try

set name of thefile to new_start & "##" & counter & "##" & thefile_name

set no_error to true

exit repeat

on error

set no_error to false

end try

end repeat

end try

end tell

return thefile

end rename_file


on delete_from_list(item2delete, thelist)

set cleanList to {}

repeat with i from 1 to count thelist

if {thelist's item i} is not in item2delete then set cleanList's end to thelist's item i

end repeat

return cleanList

end delete_from_list


on md5_list(alias_file)

--INPUT: alias files of a folder

--OUTPUT: Record list of all files within this folder (1. Level)

-- each record contains (thepath & md5)

tell application "Finder"

set fileinfos to {}

set a to original item of alias_file

if kind of a (kind of folder 1 of startup disk) then

error "ALERT, ALIAS FILE IS NOT A FOLDER STOPPED!"

else

set thefiles to every file of a

repeat with thefile in thefiles

if name of thefile does not start with "." then

if (POSIX path of (thefile as alias)) does not end with "/" then

set md5_sum to do shell script "md5 -q " & quoted form of POSIX path of (thefile as alias)

set fileinfo to {md5:md5_sum, thepath:(POSIX path of (thefile as alias)), MacOSPath:(thefile as alias)}

set fileinfos to fileinfos & {fileinfo}

end if

end if

end repeat

end if

return fileinfos

end tell

end md5_list


on checkup(script_folder)

set orphan_alias to false

set allok to false

try

tell application "Finder" to set alias_folder to folder script_folder of startup disk

on error

tell application "Finder" to set alias_folder to make new folder at startup disk with properties {name:script_folder}

tell application "Finder" to open alias_folder

tell me to activate

error script_folder & " is empty, please place an ALIAS of each folders you would like to sync into this folder.

All aliases of Source folder you be named \"SOURCE...\" and the Destination Folder should be named \"DESTINATION...\""

end try

tell application "Finder"

if (count of (every alias file of alias_folder)) > 1 then

if (count of (every alias file of alias_folder whose name starts with "SOURCE")) > 0 then

if (count of (every alias file of alias_folder whose name starts with "DESTINATION")) = 1 then

set the_aliases to every alias file of alias_folder

repeat with k in the_aliases

try

set h to original item of k

on error

delete k

set orphan_alias to true

end try

end repeat

if orphan_alias = true then

error "Hm, there is something wrong with the aliases, orphan alias files have been deleted... script stops"

else

set allok to true

end if

end if

end if

end if

end tell

if allok = true then

return allok

else

error "Hm, please check the alias folder, perhaps you have not placed all Aliases in in or maybe they are not name correctly"

end if

end checkup

No Comments