Ich muss mir immer wieder Daten aus Dokumenten ziehen, die so eigentlich nicht zu parsen sind. Bestes Beispielt war heute eine .doc-Datei, die einen Zeitplan beinhaltete und dieses Word-Konstrukt sollte nun in eine Datenbank eingefügt werden.
Kurz, das automatisch einzulesen war utopisch und jedes Feld nun via Copy&Paste zu befüllen kostet echt Zeit... Nun, ich bin zu einem Kompromiss gekommen, der mit das Leben in Zukunft auch etwas leichter machen:

Das Skript überwacht einmal gestartet die Zwischenablage. Am Anfang kann man noch eingeben wieviele Spalten die Tabelle haben soll und von da an wird jeder neue Inhalt in der Zischenablage zur Tabelle hinzugefügt. Tabelle heißt in diesem Fall eine tab-separierte Textdatei. Damit man beim Kopieren auch etwas Feedback erhält, wird jede Aktion von einem Ton begleitet und zudem sieht man in einem Terminal-Fenster, wie die Text-Datei anwächst...

Zu theoretisch? Ok, hier ein

und natürlich der Quellcode:


#Clipboard to Data Table 2016-08-19 © hubionmac.com
#Copies the clipboard contents to a text file, to make tables
# script ends as soon as you have the string "ende" in you clipboard

##Settings
set data_file_name to "clipboard_data_" & (do shell script "date '+%s'" & ".txt")
set columcount to (text returned of (display dialog "Count of Data Columns" default answer 2)) as integer
do shell script "touch ~/Desktop/" & quoted form of data_file_name
tell application "Finder" to set data_file to ((item data_file_name of desktop) as alias) as string
#show what happens to the file while the script is adding data
tell application "Terminal"
    do script "tail -f ~/Desktop/" & quoted form of data_file_name
end tell

set ccb to the clipboard
set column_pointer to 0
set theline to {}
#### OMG repeat for ever!!! ;-)
repeat until 1 is 0
    delay 1
    if ccb as text ≠ (get the clipboard) as text then
        set ccb to the clipboard as text
        ##remove newline characters from clipboard
        set tmp to my replace_chars(ccb, "\n", "")
        set tmp to my replace_chars(tmp, "\r", "")
        set theline to theline & {tmp}

        #play some feedback sound
        do shell script "afplay /System/Library/Sounds/Tink.aiff"
        if ccb is "ende" then
            # when the clipboard = ende then quit the script
            say "ende"
            exit repeat
        else
            set column_pointer to column_pointer + 1
            # when the line is full of data, write it to file and start over
            if column_pointer = columcount then
                ##join theline to tap delimited line
                set AppleScript's text item delimiters to tab
                set theline to theline as text
                set AppleScript's text item delimiters to ""
                ##write theline to file
                my write_to_file(theline & "\n", data_file, true)
                do shell script "afplay /System/Library/Sounds/Blow.aiff"
                ## reset vars for next run
                set column_pointer to 0
                set theline to {}
            end if
        end if
    end if
end repeat

on replace_chars(this_text, search_string, replacement_string)
    if this_text contains the search_string then
        set od to AppleScript's text item delimiters
        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 od
    end if
    return this_text
end replace_chars

on write_to_file(this_data, target_file, append_data)
    ##slightly tweaked routine from http://www.macosxautomation.com/applescript/sbrt/sbrt-09.html which now writes utf-8 text files =)
    try
        set the target_file to the target_file as string
        set the open_target_file to open for access file target_file with write permission
        if append_data is false then set eof of the open_target_file to 0
        write this_data to the open_target_file starting at eof as «class utf8»
        close access the open_target_file
        return true
    on error
        try
            close access file target_file
        end try
        return false
    end try
end write_to_file

Add a comment

Next Post Previous Post