URIスキーム

提供: MacWiki
移動: 案内検索

URI(URL)スキームとは?[編集]

URI は "http://macwiki.sourceforge.jp/wiki/" のようなリソース(資源)を識別する文字列で、その先頭からコロン(:)までの部分はURIスキーム(名)と呼ばれている。 詳しくは以下のところを参照して下さい。

data:(data URIスキーム)[編集]

HTML文書には
<img src="data:image/png;base64,データ">
のような感じで画像を埋め込むことができる。

PNG画像をこの形式でテキストエディットに書き込むAppleScript

set theImage to POSIX path of (choose file of type "public.png" default location (path to desktop folder) with prompt "PNGファイルを選択して下さい")
set theText to "<img src=\"data:image/png;base64," & ¬
	(do shell script "openssl base64 -e -in " & quoted form of theImage & " | tr -d '\\n'") & ¬
	"\">"
(*
set theText to "<img src=\"data:image/png;base64," & ¬
	(do shell script "python -c " & quoted form of ("print open('" & theImage & "', 'rb').read().encode('base64').replace('\\n', '')")) & ¬
	"\">"
*)
tell application "TextEdit"
	activate
	if not (exists front document) then make new document
	tell front document
		make new paragraph at after last paragraph of it with data theText
	end tell
end tell

カスタムURIスキーム[編集]

独自のURI(URL)スキームは、iOSの一部のアプリでよく利用されていて、それを使って他のアプリとの連携機能などが実現されている。Mac OS X上ではopenコマンドやAlfred、iCabなどのアプリケーションで活用できる。

アプリケーションのURI(URL)スキーム名を調べるAppleScriptテキストエディットに出力)

set appFiles to choose file of type "com.apple.application-bundle" default location (path to applications folder) with prompt "アプリケーション(.app)を選択して下さい" with multiple selections allowed
repeat with idx from 1 to (count items of appFiles)
	set item idx of appFiles to POSIX path of item idx of appFiles
end repeat

repeat with appFile in appFiles
	
	set appName to displayed name of (info for (appFile as POSIX file as alias))
	-- tell application "System Events" to set appName to displayed name of (get properties of (appFile as POSIX file as alias))
	
	set pathInfo to appFile & "Contents/Info.plist"
	tell application "System Events"
		-- get value of contents of property list file pathInfo
		set theInfo to contents of property list file pathInfo	
		set listSchemes to {}
		if (name of every property list item of theInfo) contains "CFBundleURLTypes" then
			repeat with aCFBundleURLType in (value of property list item "CFBundleURLTypes" of theInfo) as list
				set listSchemes to listSchemes & CFBundleURLSchemes of (aCFBundleURLType & {CFBundleURLSchemes:{}}) -- CFBundleURLSchemes は aCFBundleURLType になければ {} として追加
			end repeat
		end if
	end tell
	if listSchemes is {} then
		set theSchemes to "URIスキームなし"
	else
		set theSchemes to item 1 of listSchemes & ":"
		repeat with aScheme in rest of listSchemes
			set theSchemes to theSchemes & ", " & aScheme & ":"
		end repeat
	end if
	
	set theText to appName & " (" & (do shell script "basename " & quoted form of appFile) & ") => " & theSchemes & return
	tell application "TextEdit"
		try
			tell theDoc to make new paragraph at after last paragraph of it with data theText
		on error
			activate
			set theDoc to make new document
			tell theDoc to make new paragraph at after last paragraph of it with data theText
		end try
	end tell
	
end repeat

tell theDoc
	activate
	set msgText to "アプリケーション" & (count items of appFiles) & "個を調べ終わりました"
	display dialog msgText buttons {"OK"} giving up after 5 default button 1
	-- do shell script "say -v Kyoko " & msgText
end tell

Wiki内の関連ページ[編集]