#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()
####設定
property strMes : ("Xid(twitterID)を入力してください ") as text
###任意項目
property strBundleID : ("com.microsoft.edgemac") as text
set strKeyName to ("twitter.com") as text
####設定ファイル
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationSupportDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidApplicationSupportDirPathURL to ocidURLsArray's firstObject()
set ocidPreferencesDirPathURL to ocidApplicationSupportDirPathURL's URLByAppendingPathComponent:("com.cocolog-nifty.quicktimer")
#フォルダ作成
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(493) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidPreferencesDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
#設定ファイルパス
set ocidPreferencesFileBasePathURL to ocidPreferencesDirPathURL's URLByAppendingPathComponent:(strBundleID)
set ocidPlistFilePathURL to ocidPreferencesFileBasePathURL's URLByAppendingPathExtension:("plist")
##
set listPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistFilePathURL) |error|:(reference)
set ocidPlistDict to (item 1 of listPlistDict)
if ocidPlistDict = (missing value) then
log "設定ファイルが見つかりません"
###ダイアログを出す
set ocidValue to doSetTextDialogue(strBundleID)
##新規で設定ファイルを作成する
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidPlistDict's setValue:(ocidValue) forKey:(strKeyName)
set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
set listPlistData to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference)
set ocidPlistData to (item 1 of listPlistData)
#保存
set listDone to ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:0 |error|:(reference)
else
log "設定ファイルから設定を読み込みます"
set listPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistFilePathURL) |error|:(reference)
set ocidPlistDict to (item 1 of listPlistDict)
set ocidValue to ocidPlistDict's valueForKey:(strKeyName)
if ocidValue = (missing value) then
log "設定ファイルに値が見つかりません"
###ダイアログを出す
set ocidValue to doSetTextDialogue(strBundleID)
ocidPlistDict's setValue:(ocidValue) forKey:(strKeyName)
set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
set listPlistData to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference)
set ocidPlistData to (item 1 of listPlistData)
#保存
set listDone to ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:0 |error|:(reference)
end if
end if
###設定項目の戻り
set strXid to ocidValue as text
####################
## ダイアログ
####################
to doSetTextDialogue(strBundleID)
## クリップボードの中身取り出し
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPastBoardTypeArray to ocidPasteboard's types
###テキストがあれば
set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
if boolContain = true then
###値を格納する
tell application "Finder"
set strReadString to (the clipboard as text) as text
end tell
###Finderでエラーしたら
else
set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
if boolContain = true then
set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
set strReadString to ocidReadString as text
else
set strReadString to (strMes) as text
end if
end if
###ダイアログ
tell current application
set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
tell application "Finder" to activate
else
tell current application to activate
end if
##
set ocidIconPathURL to doGetAppIconPathURL(strBundleID)
set aliasIconPath to (ocidIconPathURL's absoluteURL()) as alias
set recordResult to (display dialog strMes with title "入力してください" default answer strReadString buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 20 without hidden answer) as record
if "OK" is equal to (button returned of recordResult) then
set strReturnedText to (text returned of recordResult) as text
else if (gave up of recordResult) is true then
return "時間切れです"
else
return "キャンセル"
end if
###NSStringに格納
set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
###タブと改行を除去しておく
set ocidResponseTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
ocidResponseTextM's appendString:(ocidResponseText)
##改行除去
set ocidResponseTextM to ocidResponseTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
set ocidResponseTextM to ocidResponseTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
##タブ除去
set ocidResponseTextM to ocidResponseTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
##
return ocidResponseTextM
end doSetTextDialogue
####################
## アイコンURL取得
####################
to doGetAppIconPathURL(argBundleID)
set strBundleID to argBundleID as text
set ocidAppPathURL to doGetAppPathURL(strBundleID)
if ocidAppPathURL is false then
log "アプリケーションが見つかりませんでした"
set strIconPath to ("/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/FinderIcon.icns") as text
set ocidIconPathStr to refMe's NSString's stringWithString:(strIconPath)
set ocidIconPath to ocidIconPathStr's stringByStandardizingPath()
set ocidFinderIconPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidIconPath) isDirectory:false)
return ocidIconPathURL
end if
###アイコン名をPLISTから取得
set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL)
set strIconFileName to (ocidPlistDict's valueForKey:("CFBundleIconFile")) as text
###ICONのURLにして
set strPath to ("Contents/Resources/" & strIconFileName) as text
set ocidIconFilePathURL to ocidAppPathURL's URLByAppendingPathComponent:(strPath) isDirectory:false
###拡張子の有無チェック
set strExtensionName to (ocidIconFilePathURL's pathExtension()) as text
if strExtensionName is "" then
set ocidIconFilePathURL to ocidIconFilePathURL's URLByAppendingPathExtension:"icns"
end if
###ICONファイルが実際にあるか?チェック
set appFileManager to refMe's NSFileManager's defaultManager()
set boolExists to appFileManager's fileExistsAtPath:(ocidIconFilePathURL's |path|)
###ICONがみつかない時
if boolExists is false then
return ocidFinderIconPathURL
else
return ocidIconFilePathURL
end if
end doGetAppIconPathURL
####################
## アプリケーションURL取得
####################
to doGetAppPathURL(argBundleID)
set strBundleID to argBundleID as text
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
##バンドルIDからアプリケーションのURLを取得
set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(argBundleID))
if ocidAppBundle ≠ (missing value) then
set ocidAppPathURL to ocidAppBundle's bundleURL()
else if ocidAppBundle = (missing value) then
set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(argBundleID))
end if
##予備(アプリケーションのURL)
if ocidAppPathURL = (missing value) then
tell application "Finder"
try
set aliasAppApth to (application file id strBundleID) as alias
set strAppPath to (POSIX path of aliasAppApth) as text
set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
set strAppPath to strAppPathStr's stringByStandardizingPath()
set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:true
on error
log "アプリケーションが見つかりませんでした"
return false
end try
end tell
end if
return ocidAppPathURL
end doGetAppPathURL