Top / AppleScript

uNote Scripts

uNoteはAppleScript以外にも、MacPerl (あるいはMacJPerl) を利用したPerlスクリプトで、フィルター処理することもできますね (ノートの内容を入力データとして、処理結果を同じノートに上書き出力する)。

●今日の日付の新規ノートを作成

日付の文字列の形式や作り方はいろいろありますね。以下のスクリプトでは「2000年 1月 23日 (日)」のような形式になります。
set theDate to date string of (current date) -- 今日の日付の文字列(一時的なものでこれを好みのものに加工する)
set n to number of theDate -- 今日の日付の文字列(一時的なもの)の文字数
set theDate to (items 1 thru (n - 4) of theDate as string) & " (" & (first character of last word of theDate as string) & ")"
-- 上の代わりに例えば以下のようにすると、「Jan. 23, 2000」という形式になりますね
-- ちなみに月の英語名の短縮形を得るのに単純に名前の最初の3文字を使うというやり方もされますが
-- 5月を例外的に扱う必要があったりするので、以下のように明示的にやった方がわかりやすいかも
-- もしスペイン語の月名がいいとか、別の形式にしたい場合も変更しやすいですね
-- set month_name to {"January", "February", "March", "April", "May", "June", ¬
--     "July", "August", "September", "October", "November", "December"}
-- set short_month_name to {"Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", ¬
--     "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."}
-- set theMonth to month of (current date) as string
-- repeat with mnum from 1 to count items of month_name
--     if theMonth is item mnum of month_name then
--         set theMonth to item mnum of short_month_name
--         exit repeat
--     end if
-- end repeat
-- set theDate to theMonth & " " & (day of (current date) as string) & ", " & (year of (current date) as string)
tell application "uNote 1.1.1J (PPC)"
    NewNote of front document title (theDate as string)
    -- 代わりに以下のようにすれば、前面のノートのカーソルの位置(選択部分)にテキストを挿入することができます
    -- set selection of current note of front document to (theDate & return as string)
end tell

●BibTeXのテンプレートの新規ノートを作成

Acta Classicのところでも書きましたが、BibTeXについての詳しいことはTeXの本文系のマカーにも「使える」LaTeXなどのWebページを見てください。下の方のスクリプトでActa Classicの文書に移すこともできます。
set aStr to "@book{biblabel," & return & ¬
    tab & "author = {}," & return & ¬
    tab & "editor = {}," & return & ¬
    tab & "title = {}," & return & ¬
    tab & "publisher = {}," & return & ¬
    tab & "isbn = {}," & return & ¬
    tab & "volume = {}," & return & ¬
    tab & "number = {}," & return & ¬
    tab & "series = {}," & return & ¬
    tab & "address = {}," & return & ¬
    tab & "edition = {}," & return & ¬
    tab & "month = {}," & return & ¬
    tab & "year = {}," & return & ¬
    tab & "note = {}," & return & ¬
    tab & "keyword = {}," & return & ¬
    tab & "url = {}," & return & "}"
tell application "uNote 1.1.1J (PPC)"
    NewNote of front document with aStr title "biblabel.bib"
    -- 代わりに以下のようにすれば、前面のノートのカーソルの位置(選択部分)にテキストを挿入することができます
    -- set selection of current note of front document to aStr
end tell

●選択したテキストファイルを取り込む

Finderで選択しているテキストファイルをそのファイル名をノート名にして取り込みます。ノートリストやグループリストへのファイルのドロップやインポートで楽に取り込めますが、複数のファイルを同時にドロップあるいはインポートできないので便利かと思います (新しいバージョンでは解決しているかも)。上のスクリプトと本質的に同じですね。ただし、この方法ではプレインテクスト(スタイルなし)で取り込まれます (ドラッグ&ドロップやインポートではスタイルも含めて取り込める)。
tell application "Finder"
    repeat with object in (every file of selection whose file type is "TEXT")
        set thePath to object as string
        set theName to name of object
        open for access file thePath
        set theData to read file thePath
        close access file thePath
        tell application "uNote 1.1.1J (PPC)"
            NewNote of front document with theData title theName
        end tell
    end repeat
end tell

●選択した1つのノートをクリップボードにコピー

Acta Classicでトピックを選択して、トピックをコピーするとタブで階層化したテキストがクリップボードにコピーされますが、同様のことを行います (単にノートの内容をコピーするのではなく、ノート名を付加して内容をタブでインデントしてコピーします)。Acta Classic同様、テキストのスタイルなどが失われ、プレインテクストになります。ただ、Acta Classicではトピック内に改行 (シフトキー+リターンキーで入力できる) が含まれていると、その改行の後にインデントのタブが付加されないために階層構造が崩れてしまいますが (改行の次のテキストがトップレベルに移動する)、このスクリプトではノート内に改行が含まれていてもインデントのタブを付加するので階層構造が維持されます。このスクリプトに手を加えて、タブの代わりにいくつかのスペースなどの空白文字を使ったり、電子メールソフトなどに受け渡すスクリプトを作るのもいいのではないでしょうか。
set theText to ""
tell application "uNote 1.1.1J (PPC)"
    set nid to current note of front document
    set nname to name of nid
    set theText to theText & nname & return
    set aStr to GetNote nid -- 画像が含まれたノートは、画像の部分が四角形の文字に置換されたテキストになる
    -- テキストのスタイルは失われる,失わないようにできないかな?
    set llist to every paragraph of aStr
    repeat with theLine in llist -- インデントのタブを付加するため
        set theText to theText & tab & theLine & return
    end repeat
end tell
set the clipboard to theText

●選択した1つのグループ全体をクリップボードにコピー

上のスクリプトを単純に発展させたものです。
set theText to ""
tell application "uNote 1.1.1J (PPC)"
    set gid to current group of front document
    set gname to name of gid
    set theText to theText & gname & return
    set nlist to every note of gid
    repeat with nid in nlist
        set nname to name of nid
        set aStr to GetNote nid
        set llist to every paragraph of aStr
        set isFirst to 1
        repeat with theLine in llist -- ノート内に含まれる改行の後にインデントのタブを付加するため
            if isFirst is 1 then
                set theText to theText & tab & nname & return & tab & tab & theLine & return
                set isFirst to 0
            else
                set theText to theText & tab & tab & theLine & return
            end if
        end repeat
    end repeat
end tell
set the clipboard to theText

●文書全体をクリップボードにコピー

さらに上のスクリプトを発展させました。文書が大きくなると、さすがに遅いです。
set theText to ""
tell application "uNote 1.1.1J (PPC)"
    set glist to every group of front document
    repeat with gid in glist
        set gname to name of gid
        set theText to theText & gname & return
        set nlist to every note of gid
        repeat with nid in nlist
            set nname to name of nid
            set aStr to GetNote nid
            set llist to every paragraph of aStr
            set isFirst to 1
            repeat with theLine in llist
                if isFirst is 1 then
                    set theText to theText & tab & nname & return & tab & tab & theLine & return
                    set isFirst to 0
                else
                    set theText to theText & tab & tab & theLine & return
                end if
            end repeat
        end repeat
    end repeat
end tell
set the clipboard to theText

●文書全体をActa Classic文書にコピー

上のスクリプトでクリップボードにコピーしたものをActa Classicの文書にペースト (新規トピックにペースト) することもできますが、ノートに改行が含まれている場合にそこで別々のトピックに分かれてペーストされます。このスクリプトは、ノートに改行が含まれていても1つのノートは1つのトピックとして、Acta Classicにテキストデータを移します。
-- 実行する前にコピー先のActa Classicの文書ファイルを開いておく
tell application "uNote 1.1.1J (PPC)"
    set glist to every group of front document
    repeat with gid in glist
        set gname to name of gid
        tell application "ActaClassic日本語版" to CreateTopic gname
        set nlist to every note of gid
        set isFirst to 1
        repeat with nid in nlist
            set nname to name of nid
            set aStr to GetNote nid
            if isFirst is 1 then
                tell application "ActaClassic日本語版"
                    CreateTopic nname Position daughter
                    CreateTopic aStr Position daughter
                end tell
                set isFirst to 0
            else
                tell application "ActaClassic日本語版"
                    CreateTopic nname Position aunt
                    CreateTopic aStr Position daughter
                end tell
            end if
        end repeat
    end repeat
end tell

●文書全体をテキストファイルに分割保存1

開いているuNoteの文書全体をグループをフォルダに、ノートをテキストファイルとして指定した場所に保存します。同一グループに同一の名前のノートが存在し得ますが、ノート名をファイル名として出力するので、ノート名を変更してから実行してください。ネストにするとフリーズし易いと思うので、スクリプトを分割して記述するようにしています (もっと改善できるかも)。
tell application "uNote 1.1.1J (PPC)"
    set folder_list to name of (every group of front document)
end tell
tell application "Finder"
    set cfolder to choose folder with prompt "どこに出力しますか?"
    repeat with foldername in folder_list
        make folder at folder cfolder with properties {name:foldername}
    end repeat
end tell
tell application "uNote 1.1.1J (PPC)"
    set glist to every group of front document
    repeat with gid in glist
        set gname to name of gid
        set the_folder to (cfolder & gname & ":") as string
        set nlist to every note of gid
        repeat with nid in nlist
            set nname to name of nid
            set aStr to GetNote nid
            tell application "Finder"
                make file at folder the_folder with properties {name:nname}
                set thePath to (the_folder & nname) as string
                open for access file thePath with write permission
                write aStr to thePath
                close access file thePath
                set creator type of file thePath to "MMKE"
            end tell
        end repeat
    end repeat
end tell

●文書全体をテキストファイルに分割保存2

上のスクリプトをちょっと変更して、グループごとにテキストファイル化して、各ノートはその中の一つの節とみなして、ノート名の頭に【数字】を付けてみました。好みに合わせて「第数字節」などに変更するといいでしょう。
tell application "uNote 1.1.1J (PPC)"
    set file_list to name of (every group of front document)
end tell
tell application "Finder"
    set cfolder to choose folder with prompt "どこに出力しますか?"
    repeat with filename in file_list
        make file at folder cfolder with properties {name:filename}
    end repeat
end tell
tell application "uNote 1.1.1J (PPC)"
    set glist to every group of front document
    repeat with gid in glist
        set gname to name of gid
        set nlist to every note of gid
        set theText to ""
        repeat with nid in nlist
            set nname to name of nid
            set aStr to GetNote nid
            set theText to theText & "【" & index of nid & "】" & nname & return & aStr & return
        end repeat
        tell application "Finder"
            set thePath to (cfolder & gname) as string
            open for access file thePath with write permission
            write theText to thePath
            close access file thePath
            set creator type of file thePath to "MMKE"
        end tell
    end repeat
end tell

●選択したフォルダとテキストファイルから新規文書を作成

上の「文書全体をテキストファイルに分割保存1」の逆の操作をするスクリプトです。つまり、選択されたフォルダ内・外のテキストファイルから新規のuNote文書を作ります。フォルダ名はグループ名となり、ファイル名はノート名になります。下のスクリプトは新規グループの作成の仕方がよくわからないので、uNoteがsetext形式のファイルを開けることを利用しています。あまりに汚いスクリプトで恥ずかしいですが、一応動作したので参考になれば・・・(不適切な記述なのがたまたまうまく動作しているところもあるかも)。明らかにsetextの知識があれば、setextをファイルの内容も含めて作成して、それをuNoteで開いた方がまだシンプルですね。
tell application "Finder"
    set num_text to number of every file of selection whose file type is "TEXT"
    set num_folder to number of every folder of selection
end tell
set theName to (date string of (current date)) & ".etx"
set thePath to ((path to desktop) as string) & theName
set theText to ""
repeat with num from 1 to num_folder
    set theText to theText & "group "
    set len_num to length of (num as string)
    repeat (4 - len_num) times
        set theText to theText & "0"
    end repeat
    set theText to theText & num & return & "==========" & return & return
end repeat
tell application "Finder" -- デスクトップに日付を名前に、setextのファイル (テキスト) を作る
    if (exists file thePath) then delete file thePath -- 既に同名のファイルがあれば、それはゴミ箱に捨てる
    make file at desktop with properties {name:theName}
    set theFile to open for access file theName of desktop with write permission
    write theText to theFile
    close access theFile
    set creator type of file theName of desktop to "uNOT"
    open file theName of desktop
end tell
tell application "uNote 1.1.1J (PPC)"
    set name of group theName of front document to "group 0000" -- ここにはフォルダに入っていないテキストを入れる
    set glist to every group of front document
end tell
tell application "Finder"
    if num_text is not 0 then
        repeat with object in (every file of selection whose file type is "TEXT")
            set thePath to object as string
            set theName to name of object
            open for access file thePath
            set theData to read file thePath
            close access file thePath
            tell application "uNote 1.1.1J (PPC)"
                set theNote to NewNote first item of glist with theData
                set name of theNote to theName
            end tell
        end repeat
    end if
    if num_folder is not 0 then
        set folder_list to every folder of selection
        repeat with folderid from 1 to num_folder
            set file_list to (every file of item folderid of folder_list whose file type is "TEXT")
            set num_text to number of file_list
            if num_text is not 0 then
                set folder_name to name of item folderid of folder_list
                repeat with fileid from 1 to num_text
                    set object to item fileid of file_list
                    set thePath to object as string
                    set theName to name of object
                    open for access file thePath
                    set theData to read file thePath
                    close access file thePath
                    tell application "uNote 1.1.1J (PPC)"
                        set name of item (folderid + 1) of glist to folder_name
                        set theNote to NewNote item (folderid + 1) of glist with theData
                        set name of theNote to theName
                    end tell
                end repeat
            end if
        end repeat
    end if
    delete file ((date string of (current date)) & ".etx") of desktop
end tell

Top / AppleScript