RecentTagEditor010

Warning

  • This script has been tested ONLY in Japanese

myMode

  • 1:add
  • 2:delete
  • 3:rankup

myLanguage

  • 1:English
  • 2:Japanese

source

--RecentTagEditor
--0.1.0


--config
-- myLanguage 1:English 2:Japanese
set myLanguage to 2
-- myMode 1:add 2:delete 3:rankup
set myMode to 3



tell application "Finder"
	set tmpFolder to POSIX path of ((container of (path to me)) as alias)
	--set openmeta to tmpFolder & "openmeta"
	set plistFile to POSIX path of ((path to home folder) as alias) & "Library/Preferences/com.openmeta.shared.plist"
	set tmp1 to tmpFolder & "tmp1"
	set tmp2 to tmpFolder & "tmp2"
	set tmp3 to tmpFolder & "tmp3"
	set tmp4 to tmpFolder & "tmp4"
	set ref1 to tmp1 as POSIX file
	set ref2 to tmp2 as POSIX file
	set ref3 to tmp3 as POSIX file
	set ref4 to tmp4 as POSIX file
	
	do shell script "plutil -convert xml1 -o " & tmp1 & " " & plistFile
	open for access ref1
	try
		read ref1
		set dataXML to result
	on error
		close access ref1
		return
	end try
	close access ref1
	
	if myLanguage is 1 then
		do shell script "cp " & tmp1 & " " & tmp2
	else if myLanguage is 2 then
		do shell script "iconv -c -f UTF-8 -t SHIFT_JIS " & tmp1 & " > " & tmp2
		open for access ref2
		try
			read ref2
			set dataXML to result
		on error
			close access ref2
			return
		end try
		close access ref2
	end if
end tell

set theTags to simpleTagParse(dataXML, "<array>", "</array>")
set theTags to replaceText(theTags, (ASCII character 9), "")
set theTagList to {}

repeat
	set theTag to simpleTagParse(theTags, "<string>", "</string>")
	set theTags to replaceText(theTags, "<string>" & theTag & "</string>", "")
	if theTag is missing value then exit repeat
	set the end of theTagList to theTag
end repeat

if myMode is 1 then
	--add
	set theResult to display dialog "Add" buttons {"add", "cancel"} default answer ""
	set addTag to text returned of theResult
	
	repeat with theTag in theTagList
		if theTag as text is addTag as text then error "exist"
	end repeat
	
	set beginning of theTagList to addTag
	
	--max tag?
	
else if myMode is 2 then
	--delete
	choose from list theTagList with prompt "Delete"
	set deleteTag to result
	if deleteTag is false then error "exit"
	set deleteTagList to {}
	
	repeat with theTag in theTagList
		if theTag as text is not deleteTag as text then
			set the end of deleteTagList to theTag
		end if
	end repeat
	
	set theTagList to deleteTagList
	
	
else if myMode is 3 then
	--rankup
	choose from list theTagList with prompt "Move to top"
	set moveTag to result
	if moveTag is false then error "exit"
	
	set moveTagList to {}
	set the end of moveTagList to moveTag
	
	repeat with theTag in theTagList
		if theTag as text is not moveTag as text then
			set the end of moveTagList to theTag
		end if
	end repeat
	
	set theTagList to moveTagList
	
end if

tell application "Finder"
	
	set dataXML to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" & (ASCII character 10)
	set dataXML to dataXML & "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" & (ASCII character 10)
	set dataXML to dataXML & "<plist version=\"1.0\">" & (ASCII character 10)
	set dataXML to dataXML & "<dict>" & (ASCII character 10)
	set dataXML to dataXML & "	<key>recentlyEnteredTags</key>" & (ASCII character 10)
	set dataXML to dataXML & "	<array>" & (ASCII character 10)
	
	repeat with theTag in theTagList
		set dataXML to dataXML & "		<string>" & theTag & "</string>" & (ASCII character 10)
	end repeat
	
	set dataXML to dataXML & "	</array>" & (ASCII character 10)
	set dataXML to dataXML & "</dict>" & (ASCII character 10)
	set dataXML to dataXML & "</plist>" & (ASCII character 10)
	
	open for access ref3 with write permission
	try
		write dataXML to ref3
	on error
		close access ref3
		return
	end try
	close access ref3
	
	if myLanguage is 1 then
		do shell script "cp " & tmp3 & " " & tmp4
	else if myLanguage is 2 then
		do shell script "iconv -c -f SHIFT_JIS -t UTF-8 " & tmp3 & " > " & tmp4
	end if
	
	do shell script "plutil -convert binary1 -o " & plistFile & " " & tmp4
	
	do shell script "rm \"" & tmp1 & "\""
	do shell script "rm \"" & tmp2 & "\""
	do shell script "rm \"" & tmp3 & "\""
	do shell script "rm \"" & tmp4 & "\""
	
	
end tell

--display dialog "OK"


--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