クリップボード内のテキストに改行を<br />に変更して戻す 修正
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | #com.cocolog-nifty.quicktimer.icefloe |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use framework "AppKit" |
009 | use scripting additions |
010 | |
011 | property refMe : a reference to current application |
012 | property refNSNotFound : a reference to 9.22337203685477E+18 + 5807 |
013 | |
014 | ######################## |
015 | ## クリップボードの中身取り出し |
016 | ###初期化 |
017 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
018 | set ocidPastBoardTypeArray to appPasteboard's types |
019 | ###テキストがあれば |
020 | set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text" |
021 | if boolContain = true then |
022 | ###値を格納する |
023 | tell application "Finder" |
024 | set strReadString to (the clipboard as text) as text |
025 | end tell |
026 | else |
027 | set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType" |
028 | if boolContain = true then |
029 | set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value) |
030 | set strReadString to ocidReadString as text |
031 | end if |
032 | end if |
033 | ######################## |
034 | # |
035 | set ocidReadString to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
036 | set ocidSetValue to refMe's NSString's stringWithString:(strReadString) |
037 | ocidReadString's setString:(ocidSetValue) |
038 | ############################ |
039 | #改行コード判定 |
040 | set ocidCrRange to (ocidReadString's rangeOfString:("\r"))'s location() |
041 | set ocidLfRange to (ocidReadString's rangeOfString:("\n"))'s location() |
042 | if ocidCrRange = refNSNotFound then |
043 | log "Mac改行は無い" |
044 | if ocidLfRange = refNSNotFound then |
045 | log "Mac改行もUNIX改行は無い=改行の無い1行か空のテキスト" |
046 | set ocidLength to ocidReadString's |length| |
047 | if ocidLength = (refMe's NSNumber's numberWithInteger:0) then |
048 | log "空のテキスト UNIX改行をセットします" |
049 | set ocidNewLineChar to (refMe's NSString's stringWithString:("\n")) |
050 | else |
051 | log "改行無しの1行テキスト UNIX改行をセットします" |
052 | set ocidNewLineChar to (refMe's NSString's stringWithString:("\n")) |
053 | end if |
054 | else |
055 | log "Mac改行無+UNIX改行あり=UNIX改行" |
056 | set ocidNewLineChar to (refMe's NSString's stringWithString:("\n")) |
057 | end if |
058 | else |
059 | if ocidLfRange = refNSNotFound then |
060 | log "MacがあるUNIX改行は無い=Mac改行" |
061 | set ocidNewLineChar to (refMe's NSString's stringWithString:("\r")) |
062 | else |
063 | log "Mac改行ある+UNIX改行あり=Windows改行" |
064 | set ocidNewLineChar to (refMe's NSString's stringWithString:("\r\n")) |
065 | end if |
066 | end if |
067 | #改行毎でリストにする |
068 | set ocidLineArray to (ocidReadString's componentsSeparatedByString:(ocidNewLineChar)) |
069 | ############################ |
070 | #戻し用のテキスト |
071 | set ocidSaveString to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
072 | # |
073 | repeat with itemArray in ocidLineArray |
074 | #行テキスト |
075 | (ocidSaveString's appendString:(itemArray)) |
076 | #<BR /> |
077 | set ocidSetValue to (refMe's NSString's stringWithString:("<br />")) |
078 | (ocidSaveString's appendString:(ocidSetValue)) |
079 | #改行 |
080 | (ocidSaveString's appendString:(ocidNewLineChar)) |
081 | end repeat |
082 | |
083 | set strSaveString to ocidSaveString as text |
084 | |
085 | |
086 | |
087 | ###ダイアログを前面に出す |
088 | tell current application |
089 | set strName to name as text |
090 | end tell |
091 | ####スクリプトメニューから実行したら |
092 | if strName is "osascript" then |
093 | tell application "Finder" |
094 | activate |
095 | end tell |
096 | else |
097 | tell current application |
098 | activate |
099 | end tell |
100 | end if |
101 | ###ICON |
102 | tell application "Finder" |
103 | set aliasIconPath to (POSIX file "/System/Applications/TextEdit.app/Contents/Resources/AppIcon.icns") as alias |
104 | end tell |
105 | set strTitle to ("戻り値です") as text |
106 | set strMes to ("<br />入れました") as text |
107 | 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) |
108 | |
109 | if button returned of recordResult is "クリップボードにコピー" then |
110 | set strText to text returned of recordResult as text |
111 | ####ペーストボード宣言 |
112 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
113 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
114 | appPasteboard's clearContents() |
115 | appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
116 | end if |
AppleScriptで生成しました |
| 固定リンク
« [メモ]VMware Fusion ARM Mac用 ARM版 Windows11ダウンロード | トップページ | [com.adobe.ARMDC]アクロバット・リフレッシュマネージャー Acrobat Refresh Manager の停止(少し改良) »
「NSPasteboard」カテゴリの記事
- [NSOpenPanel]対象のファイルのUTIを限定する『開く』パネル(2024.12.07)
- クリップボード内のテキストに改行を<br />に変更して戻す 修正(2024.11.15)
- ペーストボードの内容の種類の確認(2024.10.06)
- [NSPasteboard]ペーストボードからテキストの取り出し(修正)(2024.07.02)
- 画像データをクリップボードにPDF形式で入れる(2024.06.28)