ペーストボードの内容(テキスト)にBRを入れて戻す
#!/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 "Appkit"
use scripting additions
property refMe : a reference to current application
property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
########################
## クリップボードの中身取り出し
###初期化
set appPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPastBoardTypeArray to appPasteboard'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
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
end if
end if
########################
#
set ocidReadString to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
set ocidSetValue to refMe's NSString's stringWithString:(strReadString)
ocidReadString's setString:(ocidSetValue)
############################
#改行コード判定
set ocidCrRange to (ocidReadString's rangeOfString:("\r"))'s location()
set ocidLfRange to (ocidReadString's rangeOfString:("\n"))'s location()
if ocidCrRange = refNSNotFound then
log "Mac改行は無い"
if ocidLfRange = refNSNotFound then
log "Mac改行もUNIX改行は無い=改行の無い1行か空のテキスト"
set ocidLength to ocidReadString's |length|
if ocidLength = (refMe's NSNumber's numberWithInteger:0) then
log "空のテキスト UNIX改行をセットします"
set ocidNewLineChar to (refMe's NSString's stringWithString:("\n"))
else
log "改行無しの1行テキスト UNIX改行をセットします"
set ocidNewLineChar to (refMe's NSString's stringWithString:("\n"))
end if
else
log "Mac改行無+UNIX改行あり=UNIX改行"
set ocidNewLineChar to (refMe's NSString's stringWithString:("\n"))
end if
else
if ocidLfRange = refNSNotFound then
log "MacがあるUNIX改行は無い=Mac改行"
set ocidNewLineChar to (refMe's NSString's stringWithString:("\r"))
else
log "Mac改行ある+UNIX改行あり=Windows改行"
set ocidNewLineChar to (refMe's NSString's stringWithString:("\r\n"))
end if
end if
#改行毎でリストにする
set ocidLineArray to (ocidReadString's componentsSeparatedByString:(ocidNewLineChar))
############################
#戻し用のテキスト
set ocidSaveString to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
#
repeat with itemArray in ocidLineArray
#行テキスト
(ocidSaveString's appendString:(itemArray))
#<BR />
set ocidSetValue to (refMe's NSString's stringWithString:("<BR />"))
(ocidSaveString's appendString:(ocidSetValue))
#改行
(ocidSaveString's appendString:(ocidNewLineChar))
end repeat
set strSaveString to ocidSaveString as text
###ダイアログを前面に出す
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
###ICON
tell application "Finder"
set aliasIconPath to (POSIX file "/System/Applications/TextEdit.app/Contents/Resources/AppIcon.icns") as alias
end tell
set strTitle to ("戻り値です") as text
set strMes to ("<BR />入れました") as text
set recordResult to (display dialog strMes with title strTitle default answer strSaveString buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" giving up after 20 with icon aliasIconPath without hidden answer)
if button returned of recordResult is "クリップボードにコピー" then
set strText to text returned of recordResult as text
####ペーストボード宣言
set appPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
end if
| 固定リンク
« [Plist]Plistの値の変更 writeToURL:atomicallyが非推奨→ writeToURL:errorに変更 | トップページ | サウンドファイルのリネーム(データレート、サンプルレート、時間分入り) »
「NSPasteboard」カテゴリの記事
- クリップボード内のテキストに改行を<br />に変更して戻す 修正(2024.11.15)
- ペーストボードの内容の種類の確認(2024.10.06)
- [NSPasteboard]ペーストボードからテキストの取り出し(修正)(2024.07.02)
- 画像データをクリップボードにPDF形式で入れる(2024.06.28)
- クリップボードの中身で保存可能なものをファイルに保存する(2024.06.23)