オレブラウザを作る 11

2013年4月10日水曜日

AppleScript

t f B! P L
今回はJavaScript関連の実装。

JavaScript関連というかUIに関連するもの。windows.open()のダイアログとかね。WebViewDelegate.applescriptにハンドラを追加していく。

window.alert()用ダイアログの実装。コードはこんな感じ。

-- === UIDelegate ================================

on webView_runJavaScriptAlertPanelWithMessage_initiatedByFrame_(sender, message, frame)
    tell current application to display dialog (message as text) buttons {"OK"} with icon 1
end


ハンドラを追加したらこれを有効になるようWebViewのUIDelegateとWebViewDelegateを関連付け。



動作確認のhtmlを https://dl.dropbox.com/u/11714503/mywebbrowser/test.html に用意したので、これでwindow.alert()が表示されるかテスト。



意図通り表示された。次。

window.confirm()用ダイアログの実装。コードはこんな感じ。

on webView_runJavaScriptConfirmPanelWithMessage_initiatedByFrame_(sender, message, frame)
    set userCanceled to false
    try
        tell current application to display dialog (message as text) with icon 1
    on error number -128
        set userCanceled to true
    end try
    if userCanceled then
        return false
    end if
    return true
end


アプリを再起動して動作確認。



意図通り。次。

window.prompt()用ダイアログの実装。

on webView_runJavaScriptTextInputPanelWithPrompt_defaultText_initiatedByFrame_(sender, prompt, defaultText, frame)
    set userCanceled to false
    try
        tell current application to display dialog (prompt as text) default answer (defaultText as text) with icon 1
        set textReturned to text returned of result
    on error number -128
        set userCanceled to true
    end try
    if userCanceled then
        return
    end if
    return textReturned
end



動作確認。意図通り。textReturnedに入力値が入ってくるのもlog出力とかで確認できる。ダイアログは通常のAppleScriptコマンドをそのまま使えるから楽。

次。

<input type="file">のファイル選択ダイアログを実装。これもそんなに難しいものじゃない。

on webView_runOpenPanelForFileButtonWithResultListener_allowMultipleFiles_(sender, resultListener, allowMultipleFiles)
    if allowMultipleFiles then
        set fileList to {}
        tell current application to choose file with multiple selections allowed
        repeat with aFile in result
            tell application "Finder" to POSIX path of aFile
            set the end of fileList to result
        end repeat
        tell resultListener to chooseFilenames_(fileList)
    else
        tell current application to choose file
        tell application "Finder" to POSIX path of result
        tell resultListener to chooseFilename_(result)
    end if
end


choose file はAppleScriptのファイル選択ダイアログを使用する定型句。Cocoa-AppleScriptアプリケーションではファイルパス形式をPOSIXで扱うので、その形式に変換することに注意。

これも意図通りに動いた。



基本的なUI周りの実装はこれくらい。今回はここまで。

次回はウェブコンテンツのソース表示やJavaScriptデバッグを行うweb inspector機能を組み込む。





このブログを検索

人気の投稿

ラベル

QooQ