SaveWithMetaP080.app

  • pixivからOpenMetaタグ付きで画像を保存します
    • 作者名、作品名、登録タグが並びます

以下、ソース

--SaveWithMetaP
--0.8.0


tell application "Finder"
	set tmpFolder to POSIX path of ((container of (path to me)) as alias)
	set openmeta to tmpFolder & "openmeta" --openmetaパス
end tell
set theTags to ""

tell application "Safari"
	set theSource to (the source of front document)
end tell

--本画像表示htmlへのURL抽出
set fullURL to simpleTagParse(theSource, "<div style=\"text-align:center;padding:10px 0;\">", "</div>")
set fullURL to simpleTagParse(fullURL, "\"", "\"")
set fullURL to replaceText(fullURL, "middle", "big")
set fullURL to "http://www.pixiv.net/" & fullURL

--本画像表示htmlへアクセス
---URL Access Scriptingではセッション継続できない
tell application "Safari"
	open location fullURL
	delay 3
	repeat with timer from 1 to 20
		try
			set theFullSource to (the source of document 1)
		on error
			delay 1
		end try
	end repeat
end tell

--本画像へのURL抽出
set theImageURL to simpleTagParse(theFullSource, "<div  id=\"illust_contents\" style=\"padding-top:10px;text-align:center;\">", "</div>")
set theImageURL to simpleTagParse(theImageURL, "<img src=\"", "\"")
--ファイル名
set fLen to length of theImageURL
set revText to (reverse of (characters of theImageURL)) as string
set anOffset to offset of "/" in revText
set theFileName to text (fLen - anOffset + 2) thru fLen of theImageURL

tell application "Finder"
	set saveFilePath to POSIX path of ((path to desktop) as alias) & theFileName
	set saveFileRef to saveFilePath as POSIX file
end tell

--本画像保存
tell application "Safari"
	do JavaScript "window.close();" in document 1
	open location theImageURL
	repeat with timer from 1 to 20
		try
			
		on error
			delay 1
		end try
	end repeat
	
	save front document in saveFileRef
	
	do JavaScript "window.close();" in document 1
end tell

--作者とタイトル
set theAuthor to simpleTagParse(theSource, "<div id=\"profile\">", "<div class=\"profile_edit\">")
set theAuthor to simpleTagParse(theAuthor, "<br />", "</div>")
set theAuthor to replaceText(theAuthor, (ASCII character 9), "")
set theAuthor to replaceText(theAuthor, (ASCII character 10), "")
set theTags to "\"" & theAuthor & "\""

set theTitle to simpleTagParse(theSource, "<td style=\"width:430px;\" valign=\"top\">", "</td>")
set theTitle to simpleTagParse(theTitle, "<div class=\"f18b\">", "</div>")
set theTags to theTags & " \"" & theTitle & "\""

--タグ
set theSource to simpleTagParse(theSource, "<div id=\"tag_area\"", "</div>")
set theSource to replaceText(theSource, "<span style=\"color:#999;\">*</span>", "")
set theSource to simpleTagParse(theSource, "<span id=\"tags\">", "</span>")
set theSource to replaceText(theSource, (ASCII character 9), "")

repeat
	set theTag to simpleTagParse(theSource, "\">", "</")
	set theSource to replaceText(theSource, "\">" & theTag & "</", "")
	set theSource to replaceText(theSource, "\"></", "")
	if theTag is missing value then exit repeat
	set theTags to theTags & " \"" & theTag & "\""
end repeat

-- `を排除 他の記号も必要かも
set theTags to replaceText(theTags, "`", "")

--タグ付け
do shell script openmeta & " -a " & theTags & " -p \"" & saveFilePath & "\""



--引用元
--http://homepage.mac.com/travellers/blog/C746134881/E441300314/index.html
--ちょっと改造
on simpleTagParse(tmpData, beginKey, endKey)
	set oldDelim to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to beginKey
		set tmpData2 to second text item of tmpData
		set AppleScript's text item delimiters to endKey
		set tmpData3 to (first text item of tmpData2) as Unicode text
		set AppleScript's text item delimiters to oldDelim
		return tmpData3
	on error
		if tmpData does not contain beginKey then
			log "The key doesn't exist in the data"
		end if
		set AppleScript's text item delimiters to oldDelim
		return missing value
	end try
end simpleTagParse


--引用元
--http://www.tonbi.jp/AppleScript/tips/String/FindReplace.html
on replaceText(theText, serchStr, replaceStr)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to serchStr
	set theList to every text item of theText
	set AppleScript's text item delimiters to replaceStr
	set theText to theList as string
	set AppleScript's text item delimiters to tmp
	return theText
end replaceText