#!/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
########################
###設定項目
########################
###自分のID
set strMyTwiiterID to ("floe_ice") as text
###開くアプリケーション
set strBundleID to "com.microsoft.edgemac" as text
(* ここは好みで変更
com.apple.Safari
com.google.Chrome
org.mozilla.firefox
*)
########################
## クリップボードの中身取り出し
########################
###初期化
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
log "テキストなし"
set strReadString to "検索文字列を入力" 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"
activate
end tell
else
tell current application
activate
end tell
end if
set aliasIconPath to (POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MagnifyingGlassIcon.icns") as alias
try
set strMes to "検索文字列を入力してください\nユーザー指定:@" & strMyTwiiterID & "でtwitter検索します" as text
set recordResult to (display dialog strMes with title "入力してください" default answer strReadString buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 10 without hidden answer) as record
on error
log "エラーしました"
return
end try
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
################################
## 本処理
################################
set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
###タブと改行を除去しておく
set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
ocidTextM's appendString:(ocidResponseText)
##改行除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
##タブ除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
set strReturnedText to ocidTextM as text
##検索文字列にする
set strSearchString to ("@" & strMyTwiiterID & " " & strReturnedText) as text
##@を%エンコードしたいならこちら
## set strSearchString to ("%40" & strMyTwiiterID & " " & strReturnedText) as text
set ocidViewURLString to refMe's NSString's stringWithString:(strSearchString)
##クエリーアイテムにしておく
set ocidQueryItemQ to refMe's NSURLQueryItem's alloc()'s initWithName:("q") value:(ocidViewURLString)
##クエリーアイテム『ズ』でセットするのArrayに入れておく
set ocidQueryItems to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidQueryItems's addObject:(ocidQueryItemQ)
##TOPの場合
#set ocidQueryItemF to refMe's NSURLQueryItem's alloc()'s initWithName:("f") value:("top")
##Latestの場合
set ocidQueryItemF to refMe's NSURLQueryItem's alloc()'s initWithName:("f") value:("live")
ocidQueryItems's addObject:(ocidQueryItemF)
##
set ocidQueryItemSRC to refMe's NSURLQueryItem's alloc()'s initWithName:("src") value:("typed_query")
ocidQueryItems's addObject:(ocidQueryItemSRC)
##############
###送信するURL
set strURL to ("https://twitter.com/search") as text
set ocidURL to refMe's NSURL's URLWithString:(strURL)
###コンポーネントに分解
set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:(ocidURL) resolvingAgainstBaseURL:(true)
##クエリーをセットする
ocidComponents's setQueryItems:(ocidQueryItems)
##URLに戻して テキストにしておく
set ocidOpenURL to ocidComponents's |URL|()
set strOpenURL to ocidOpenURL's absoluteString() as text
################################
## OPEN
################################
tell application id strBundleID
open location strOpenURL
end tell
####################################
###### %エンコード
####################################
on doUrlEncode(argText)
##テキスト
set ocidArgText to refMe's NSString's stringWithString:(argText)
##キャラクタセットを指定
set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
##キャラクタセットで変換
set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
##テキスト形式に確定
set strTextToEncode to ocidArgTextEncoded as text
###値を戻す
return strTextToEncode
end doUrlEncode
####################################
###### %エンコード
####################################
on doTextEncode(argText)
##テキスト
set ocidArgText to refMe's NSString's stringWithString:(argText)
##キャラクタセットを指定
set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
##キャラクタセットで変換
set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
######## 置換 %エンコードの追加処理
###置換レコード
set recordPercentMap to {|!|:"%21", |#|:"%23", |$|:"%24", |&|:"%26", |'|:"%27", |(|:"%28", |)|:"%29", |*|:"%2A", |+|:"%2B", |,|:"%2C", |:|:"%3A", |;|:"%3B", |=|:"%3D", |?|:"%3F", |@|:"%40", | |:"%20", |/|:"%2F"} as record
###ディクショナリにして
set ocidPercentMap to refMe's NSDictionary's alloc()'s initWithDictionary:(recordPercentMap)
###キーの一覧を取り出します
set ocidAllKeys to ocidPercentMap's allKeys()
###取り出したキー一覧を順番に処理
repeat with itemAllKey in ocidAllKeys
set strItemKey to itemAllKey as text
##キーの値を取り出して
if strItemKey is "@" then
##置換
set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:("@") withString:("%40"))
else
set ocidMapValue to (ocidPercentMap's valueForKey:(strItemKey))
##置換
set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:(strItemKey) withString:(ocidMapValue))
end if
##次の変換に備える
set ocidArgTextEncoded to ocidEncodedText
end repeat
##テキスト形式に確定
set strTextToEncode to ocidEncodedText as text
###値を戻す
return strTextToEncode
end doTextEncode
####################################
###### %デコード
####################################
on doUrlDecode(argText)
##テキスト
set ocidArgText to refMe's NSString's stringWithString:(argText)
##デコード
set ocidArgTextEncoded to ocidArgText's stringByRemovingPercentEncoding
set strArgTextEncoded to ocidArgTextEncoded as text
return strArgTextEncoded
end doUrlDecode