AppleScript NSString

TextClippingファイルの内容をUTIを選択して取得する(仮)

もっと他にいい方法がありそうな気がするので仮

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
# com.cocolog-nifty.quicktimer.icefloe
(*
public.htmlだけはHTMLソースを受け取ります


*)
----+----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


on run
  ###ダイアログを前面に出す
  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 appFileManager to refMe's NSFileManager's defaultManager()
  set ocidUserDesktopPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
  set ocidUserDesktopPath to ocidUserDesktopPathArray's firstObject()
  set aliasDesktopDirPath to (ocidUserDesktopPath's absoluteURL()) as alias
  ##対象のUTI
  set listChooseFileUTI to {"com.apple.finder.textclipping"}
  set strPromptText to "textClippingファイルを選んでください" as text
  set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDesktopDirPath) of type listChooseFileUTI with invisibles without showing package contents and multiple selections allowed) as list
open listAliasFilePath
end run

on open listAliasFilePath
  
  set aliasFilePath to (item 1 of listAliasFilePath) as alias
  set strFilePath to (POSIX path of aliasFilePath) as text
  set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
  ##拡張子
  set strExtensionName to ocidFilePathURL's pathExtension() as text
  if strExtensionName ≠ "textClipping" then
log "クリッピング以外は処理しません"
    exit repeat
  end if
  #クリッピングテキストを取得して
  set ocidPlistDict to (refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL))
  #UTIデータを取り出します
  set ocidUTIDATA to (ocidPlistDict's objectForKey:("UTI-Data"))
  #収容されているキーの名前をリストで取得して
  set ocidAllKeys to ocidUTIDATA's allKeys()
  set listAllKeys to ocidAllKeys as list
  ###ダイアログを前面に出す
  set strName to (name of current application) as text
  if strName is "osascript" then
    tell application "Finder" to activate
  else
    tell current application to activate
  end if
  set strTitle to ("選んでください") as text
  set strPrompt to ("取得するデータ形式を選んでください") as text
  try
    set listResponse to (choose from list listAllKeys with title strTitle with prompt strPrompt default items (item 1 of listAllKeys) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
  on error
log "エラーしました"
return "エラーしました"
  end try
  
  if (item 1 of listResponse) is false then
return "キャンセルしましたA"
  else if (item 1 of listResponse) is "キャンセル" then
return "キャンセルしましたB"
  end if
  ##これが選んだUTI
  set strGetKey to (item 1 of listResponse) as text
  ##UTI-Dataから指定のデータを取り出して
  set ocidGetValue to (ocidUTIDATA's valueForKey:(strGetKey))
  set strClassName to (ocidGetValue's className()) as text
log strClassName
  if strClassName contains "String" then
    if strGetKey is "public.utf8-plain-text" then
      #通常のテキストとして格納します
      set ocidOption to (refMe's NSUTF8StringEncoding)
      set ocidStrings to refMe's NSString's stringWithString:(ocidGetValue)
    else if strGetKey is "public.rtf" then
      #AttributedStringにしてからテキストデータのみ取得します
      set ocidRtfData to (ocidGetValue's dataUsingEncoding:(refMe's NSUTF8StringEncoding))
      set ocidAttarDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
      set ocidKey to (refMe's NSDocumentTypeDocumentAttribute)
      set ocidValue to (refMe's NSRTFTextDocumentType)
      set listAttributedString to (refMe's NSAttributedString's alloc()'s initWithData:(ocidRtfData) options:(ocidAttarDict) documentAttributes:(missing value) |error|:(reference))
      set ocidAttributedString to (item 1 of listAttributedString)
      set ocidStrings to ocidAttributedString's |string|()
    end if
  else if strClassName contains "Data" then
log "データ処理します"
    if strGetKey is "com.apple.traditional-mac-plain-text" then
      #通常のテキストとして格納します
      set ocidOption to (refMe's NSMacOSRomanStringEncoding)
      set ocidStrings to (refMe's NSString's alloc()'s initWithData:(ocidGetValue) encoding:(ocidOption))
    else if strGetKey is "public.utf8-plain-text" then
      #通常のテキストとして格納します
      set ocidOption to (refMe's NSUTF8StringEncoding)
      set ocidStrings to (refMe's NSString's alloc()'s initWithData:(ocidGetValue) encoding:(ocidOption))
    else if strGetKey is "public.utf16-plain-text" then
      #通常のテキストとして格納します
      set ocidOption to (refMe's NSUTF16LittleEndianStringEncoding)
      set ocidStrings to (refMe's NSString's alloc()'s initWithData:(ocidGetValue) encoding:(ocidOption))
    else if strGetKey is "public.utf16-external-plain-text" then
      #通常のテキストとして格納します
      set ocidOption to (refMe's NSUnicodeStringEncoding)
      set ocidStrings to (refMe's NSString's alloc()'s initWithData:(ocidGetValue) encoding:(ocidOption))
    else if strGetKey is "public.html" then
      #HTMLのソースとして格納します
      set ocidAttarDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
      set ocidKey to (refMe's NSDocumentTypeDocumentAttribute)
      set ocidValue to (refMe's NSHTMLTextDocumentType)
(ocidAttarDict's setObject:(ocidValue) forKey:(ocidKey))
      set ocidAttributedString to (refMe's NSAttributedString's alloc()'s initWithHTML:(ocidGetValue) options:(ocidAttarDict) documentAttributes:(missing value))
      set ocidLength to ocidAttributedString's |length|()
      set ocidDataRange to refMe's NSMakeRange(0, ocidLength)
      set listHTMLData to (ocidAttributedString's dataFromRange:(ocidDataRange) documentAttributes:(ocidAttarDict) |error|:(reference))
      set ocidHTMLData to (item 1 of listHTMLData)
      set ocidStrings to (refMe's NSString's alloc()'s initWithData:(ocidHTMLData) encoding:(refMe's NSUTF8StringEncoding))
    end if
  end if
  
  set strSetDialogText to ocidStrings 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
  
  set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ClippingText.icns" as alias
  try
    set recordResponse to (display dialog "テキスト内容です" with title "テキストデータです" default answer (strSetDialogText) buttons {"クリップボードにコピー", "ファイルに保存", "キャンセル"} default button "ファイルに保存" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
  on error
log "エラーしました"
return "エラーしました"
  end try
  if true is equal to (gave up of recordResponse) then
return "時間切れですやりなおしてください"
  end if
  if "ファイルに保存" is equal to (button returned of recordResponse) then
    #保存先パスと拡張子の組み合わせ
    set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
    set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
    if strGetKey is "public.html" then
      set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("html")
      set listDone to ocidHTMLData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)
    else if strGetKey is "public.rtf" then
      set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("rtf")
      set listDone to ocidRtfData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)
    else
      set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("txt")
      set listDone to ocidStrings's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(ocidOption) |error|:(reference)
    end if
  else if button returned of recordResponse is "クリップボードにコピー" then
    set strText to text returned of recordResponse 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)
return
  else
log "キャンセルしました"
return "キャンセルしました"
  end if
  
  
return
  
end open

|

TSV2HTML タブ区切りテキストをHTMLテーブルに変換

修正版があります
TSV2HTML タブ区切りテキストをHTMLテーブルに変換 UNIX改行修正
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-d6338f.html

【スクリプトエディタで開く】|

#!/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

### 【1】入力ファイル
#ダイアログ
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 appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
#
set listUTI to {"public.tab-separated-values-text"}
set strMes to ("ファイルを選んでください") as text
set strPrompt to ("ファイルを選んでください") as text
try
  ### ファイル選択時
  set aliasFilePath to (choose file strMes with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
if aliasFilePath is (missing value) then
return "選んでください"
end if
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
#保存するタブ区切りテキストのパス
set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
set ocidFileName to ocidBaseFilePathURL's lastPathComponent()
set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("html"))
### 【2】 ファイルのテキストを読み込み
set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
set ocidReadString to (item 1 of listResponse)
#可変にして
set ocidLineString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
(ocidLineString's setString:(ocidReadString))
#改行をUNIXに強制
set ocidLineStringLF to (ocidLineString's stringByReplacingOccurrencesOfString:("\r\n") withString:("\n"))
set ocidLineString to (ocidLineStringLF's stringByReplacingOccurrencesOfString:("\r") withString:("\n"))
#改行毎でリストにする
set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet)
set ocidLineArray to (ocidLineString's componentsSeparatedByCharactersInSet:(ocidCharSet))
#最初の1行目だけ別で取得しておく
set ocidFirstObjectString to ocidLineArray's firstObject()
set ocidFirstLineArray to ocidFirstObjectString's componentsSeparatedByString:("\t")

###【4】データをHTMLに整形
########################################
#headerに渡すエレメント
set ocidH3Element to refMe's NSXMLElement's elementWithName:("h3")
(ocidH3Element's setStringValue:("タブ区切りテキストをHTMLテーブルに"))
########################################
#footerに渡すエレメント
set ocidFotterAElement to refMe's NSXMLElement's elementWithName:("a")
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://quicktimer.cocolog-nifty.com/"))
(ocidFotterAElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidFotterAElement's addAttribute:(ocidAddNode))
set strContents to ("AppleScriptで生成しました") as text
(ocidFotterAElement's setStringValue:(strContents))
########################################
#articleに渡すエレメント
#テーブル部生成開始
set ocidTableElement to refMe's NSXMLElement's elementWithName:("table")
#【caption】
set ocidCaptionElement to refMe's NSXMLElement's elementWithName:("caption")
ocidCaptionElement's setStringValue:(ocidFileName)
ocidTableElement's addChild:(ocidCaptionElement)
#【colgroup】
set ocidColgroupElement to refMe's NSXMLElement's elementWithName:("colgroup")
#タイトル部の数だけ繰り返し
repeat with itemColName in ocidFirstLineArray
  #【col】col生成
  set ocidAddElement to (refMe's NSXMLElement's elementWithName:("col"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
(ocidColgroupElement's addChild:(ocidAddElement))
end repeat
#テーブルエレメントに追加
ocidTableElement's addChild:(ocidColgroupElement)
#【thead】
set ocidTheadElement to refMe's NSXMLElement's elementWithName:("thead")
#TR
set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr")
#タイトル部の数だけ繰り返し
repeat with itemColName in ocidFirstLineArray
  #ここはTDではなくてTHを利用
  set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
  #
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("id") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidAddElement's addAttribute:(ocidAddNode))
  #値を入れる
(ocidAddElement's setStringValue:(itemColName))
  #TH→TRにセット
(ocidTrElement's addChild:(ocidAddElement))
end repeat
#TRをTHEADにセット
ocidTheadElement's addChild:(ocidTrElement)
#THEADをテーブルにセット
ocidTableElement's addChild:(ocidTheadElement)
########################################
#【tbody】
set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody")
###【3-4】:item
set numCntContents to (count of ocidLineArray) - 1 as integer
repeat with itemIntNo from 1 to numCntContents by 1
  set ocidLineItem to (ocidLineArray's objectAtIndex:(itemIntNo))
  #空行で終わり
  if (ocidLineItem as text) is "" then
    exit repeat
  end if
  #
  set ocidItemLineArray to (ocidLineItem's componentsSeparatedByString:("\t"))
  set numCntItemLineArray to (count of ocidItemLineArray) as integer
  ##############
  #TRの開始
  set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
  repeat with itemLineNo from 0 to (numCntItemLineArray - 1) by 1
    set coidFieldValue to (ocidItemLineArray's objectAtIndex:(itemLineNo))
    if itemLineNo = 0 then
      set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
      set strTitle to (ocidFirstLineArray's objectAtIndex:(itemLineNo))
      set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(strTitle))
(ocidThElement's addAttribute:(ocidAddNode))
      set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("headers") stringValue:(strTitle))
(ocidThElement's addAttribute:(ocidAddNode))
      set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidThElement's addAttribute:(ocidAddNode))
(ocidThElement's setStringValue:(coidFieldValue))
(ocidTrElement's addChild:(ocidThElement))
    else
      set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
      set strTitle to (ocidFirstLineArray's objectAtIndex:(itemLineNo))
      set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(strTitle))
(ocidTdElement's addAttribute:(ocidAddNode))
      set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("headers") stringValue:(strTitle))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(coidFieldValue))
(ocidTrElement's addChild:(ocidTdElement))
    end if
  end repeat
(ocidTbodyElement's addChild:(ocidTrElement))
end repeat
#TBODYをテーブルにセット
ocidTableElement's addChild:(ocidTbodyElement)
#【tfoot】 TRで
set ocidTfootElement to refMe's NSXMLElement's elementWithName:("tfoot")
set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr")
#項目数を取得して
set numCntCol to (count of ocidFirstLineArray) as integer
#colspan指定して1行でセット
set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("テーブルの終わり"))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("colspan") stringValue:(numCntCol as text))
(ocidThElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row"))
(ocidThElement's addAttribute:(ocidAddNode))
#
set strContents to ("項目数 : " & (numCntContents - 1)) as text
(ocidThElement's setStringValue:(strContents))
#THをTRにセットして
ocidTrElement's addChild:(ocidThElement)
#TRをTFOOTにセット
ocidTfootElement's addChild:(ocidTrElement)
#TFOOTをテーブルにセット
ocidTableElement's addChild:(ocidTfootElement)

##############################
#HTMLにする
##############################
set ocidHTML to doMakeRootElement({ocidH3Element, ocidTableElement, ocidFotterAElement})

##############################
#保存
##############################

#####【5−2】保存
#読み取りやすい表示
set ocidXMLdata to ocidHTML's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)

set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)

####【6】ブラウザで開く
set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
tell application "Finder"
open location aliasFilePath
end tell

############################################################
# 基本的なHTMLの構造
(*
doMakeRootElement({argHeaderContents, argArticleContents, argFooterContents})
HTMLのBODY部
header
article
footerにそれぞれAddchildするデータをリストで渡す
戻り値はRootエレメントにセットされた
NSXMLDocumentを戻すので 保存すればOK
*)
############################################################
to doMakeRootElement({argHeaderContents, argArticleContents, argFooterContents})
  #XML初期化
  set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init()
ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentHTMLKind)
  # DTD付与
  set ocidDTD to refMe's NSXMLDTD's alloc()'s init()
ocidDTD's setName:("html")
ocidXMLDoc's setDTD:(ocidDTD)
  #
  set ocidRootElement to refMe's NSXMLElement's elementWithName:("html")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("lang") stringValue:("ja")
ocidRootElement's addAttribute:(ocidAddNode)
  #
  set ocidHeadElement to refMe's NSXMLElement's elementWithName:("head")
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("title")
ocidAddElement's setStringValue:("TSV2HTML")
ocidHeadElement's addChild:(ocidAddElement)
  # http-equiv
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/html; charset=UTF-8")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Style-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/css")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Script-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/javascript")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("name") stringValue:("viewport")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("width=720")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("style")
ocidAddElement's setStringValue:("body { margin: 10px; background-color: #FFFFFF; } table { border-spacing: 0; caption-side: top; font-family: system-ui; } thead th { border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; &:first-of-type { border-start-start-radius: .5em } &:last-of-type { border-start-end-radius: .5em; border-inline-end-width: 1px } } tbody td { word-wrap: break-word;max-width: 360px;border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; &:last-of-type { border-inline-end-width: 1px } } tbody th { border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; } tbody tr:nth-of-type(odd) { background: #F2F2F2; } .kind_string { font-size: 0.75em; } .date_string { font-size: 0.5em; } tfoot th { border: solid 1px #666666; padding: .5ch 1ch; &:first-of-type { border-end-start-radius: .5em } &:last-of-type { border-end-end-radius: .5em; border-inline-end-width: 1px } }")
ocidHeadElement's addChild:(ocidAddElement)
ocidRootElement's addChild:(ocidHeadElement)
  #
  #ボディエレメント
  set ocidBodyElement to refMe's NSXMLElement's elementWithName:("body")
  #ヘッダー
  set ocidHeaderElement to refMe's NSXMLElement's elementWithName:("header")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("header")
ocidHeaderElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_header")
ocidHeaderElement's addAttribute:(ocidAddNode)
ocidHeaderElement's addChild:(argHeaderContents)
ocidBodyElement's addChild:(ocidHeaderElement)
  #アーティクル
  set ocidArticleElement to refMe's NSXMLElement's elementWithName:("article")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("article")
ocidArticleElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_article")
ocidArticleElement's addAttribute:(ocidAddNode)
ocidArticleElement's addChild:(argArticleContents)
ocidBodyElement's addChild:(ocidArticleElement)
  #フッター
  set ocidFooterElement to refMe's NSXMLElement's elementWithName:("footer")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("footer")
ocidFooterElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_footer")
ocidFooterElement's addAttribute:(ocidAddNode)
ocidFooterElement's addChild:(argFooterContents)
ocidBodyElement's addChild:(ocidFooterElement)
  #ボディをROOTエレメントにセット
ocidRootElement's addChild:(ocidBodyElement)
  #ROOTをXMLにセット
ocidXMLDoc's setRootElement:(ocidRootElement)
  #値を戻す
return ocidXMLDoc
end doMakeRootElement

|

[NSString]空のテキストファイルを作る writeToFile:atomically:encoding:error:

【スクリプトエディタで開く】

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()

set ocidTempDirURL to appFileManager's temporaryDirectory()

set ocidMakeDirURL to ocidTempDirURL's URLByAppendingPathComponent:"_DislUtil" isDirectory:true
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidMakeDirURL) withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
set ocidFilePathURL to ocidMakeDirURL's URLByAppendingPathComponent:"diskutil.txt" isDirectory:false

###空のファイルを作成する
set ocidBlankText to refMe's NSString's stringWithString:""
ocidBlankText's writeToURL:ocidFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)

|

【characterSetWithCharactersInString】改行コード判定

何か他にいい方法があるような気もする

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

# com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos12なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions


property refMe : a reference to current application


tell application "CotEditor"

  tell front document

    set strText to contents

    ####改行コード取得

    set valueNewLineChr to (line ending)

  end tell

  if valueNewLineChr is CRLF then

    log "改行コードはCRLF=WINDOWSです"

  else if valueNewLineChr is CR then

    log "改行コードはCR=MacOSです"

  else if valueNewLineChr is LF then

    log "改行コードはLF=UNIXです"

  end if

end tell


set strText to strText as text



set ocidText to refMe's NSString's stringWithString:strText

############################

######改行毎でリスト化

############################

######改行コード

set boolCRLF to ocidText's containsString:"\r\n"

set boolCR to ocidText's containsString:"\r"

#####格納用リストの初期化

set ocidTextArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0

#####改行コード判定

if boolCRLF is true then

  log "改行コードはCRLF=WINDOWSです"

  set ocidTextArray to ocidText's componentsSeparatedByString:"\r\n"

else if boolCR is true then

  log "改行コードはCR=MacOSです"

  set ocidLineEndChrSet to refMe's NSCharacterSet's characterSetWithCharactersInString:"\r"

  set ocidTextArray to ocidText's componentsSeparatedByCharactersInSet:ocidLineEndChrSet

else

  set boolLF to ocidText's containsString:"\n"

  if boolLF is true then

    log "改行コードはLF=UNIXです"

    set ocidLineEndChrSet to refMe's NSCharacterSet's characterSetWithCharactersInString:"\n"

    set ocidTextArray to ocidText's componentsSeparatedByCharactersInSet:ocidLineEndChrSet

  else

    log "改行の無い1行の文字列です"

  end if

end if

|

[表示]大文字 小文字 キャピタライズ

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString


set strText to "ABCDEFGHIJK" as text
set ocdiText to refNSString's stringWithString:strText

####小文字
set ocidLowerCase to ocdiText's lowercaseString()
log ocidLowerCase as text

####大文字
set ocidLowerCase to ocidLowerCase's uppercaseString()
log ocidLowerCase as text

####先頭大文字
set ocidLowerCase to ocidLowerCase's capitalizedString()
log ocidLowerCase as text

|

[% Decode] stringByRemovingPercentEncoding %デコード

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

######ログ表示
doLogView()

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString

## set text
set strURL to "http%3A%2F%2Ffoo%20bar%2F" as text

##Make NSString
set ocidNSString to objNSString's stringWithString:strURL
log ocidNSString as text
log ocidNSString's className() as text
-->(*__NSCFString*)

### Do Percent Encoding
set ocidEncodedURL to ocidNSString's stringByRemovingPercentEncoding()
log ocidEncodedURL as text
log ocidEncodedURL's className() as text
-->(*__NSCFString*)

return ocidEncodedURL as text
-->"http://foo bar/"




#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

[NSMutableString] deleteCharactersInRange

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

######ログ表示
doLogView()

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSMutableString : a reference to objMe's NSMutableString
property objNSRange : a reference to objMe's NSRange



set strText to "美しい日本語"

set ocidNSString to objNSString's alloc()'s initWithString:strText
log ocidNSString as text
log ocidNSString's className() as text
-->(*__NSCFString*)

set ocidMutableString to objNSMutableString's alloc()'s initWithCapacity:0
ocidMutableString's setString:strText
log ocidMutableString as text
log ocidMutableString's className() as text
-->(*__NSCFString*)


ocidMutableString's insertString:"" atIndex:5
log ocidMutableString as text
log ocidMutableString's className() as text
-->(*__NSCFString*)

#####テキストからRangeを作成-->3文字目以降の4文字の意味
set ocidNSRangeFromString to objNSRange's NSRangeFromString("3 4")
log class of ocidNSRangeFromString
log ocidNSRangeFromString as record

ocidMutableString's deleteCharactersInRange:ocidNSRangeFromString
log ocidMutableString as text
log ocidMutableString's className() as text
-->(*__NSCFString*)


#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

[NSString] initWithString

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

######ログ表示
doLogView()

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSMutableString : a reference to objMe's NSMutableString




set strText to "美しい日本語"

set ocidNSString to objNSString's alloc()'s initWithString:strText
log ocidNSString as text
log ocidNSString's className() as text
-->(*__NSCFString*)

set ocidMutableString to objNSMutableString's alloc()'s initWithCapacity:0
ocidMutableString's setString:strText
log ocidNSString as text
log ocidNSString's className() as text
-->(*__NSCFString*)






#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

[NSString] stringByStandardizingPath

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

######ログ表示
doLogView()

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL


property objNSNotFound : a reference to 9.22337203685477E+18 + 5807

set objFileManager to objMe's NSFileManager's defaultManager()



set strPosixPath to "/Library/Documentation/Warranty.rtf"
log strPosixPath as text
log class of strPosixPath
-->(*/Library/Documentation/Warranty.rtf*)
-->(*text*)

set ocidPosixPath to objNSString's stringWithString:strPosixPath
log ocidPosixPath as text
log className() of ocidPosixPath as text
(*/Library/Documentation/Warranty.rtf*)
-->(*__NSCFString*)


set ocidPosixFullPath to ocidPosixPath's stringByStandardizingPath
log ocidPosixFullPath as text
log className() of ocidPosixFullPath as text
(*/Library/Documentation/Warranty.rtf*)
-->(*NSPathStore2*)



#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

[fig 02-04]テキストの結合(追加)

#!/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 scripting additions
####ログ表示
doLogView()
####
property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSMutableString : a reference to objMe's NSMutableString

set strTextA to "美しい日本語" as text
set strTextB to "" as text
set strTextC to "オープンタイプフォント" as text


set strTextD to (strTextA & strTextB & strTextC) as text
log strTextD as text
log class of strTextD

set strTextE to (strTextA & strTextB & "" & strTextC) as text

log strTextE as text
log class of strTextE

########################

set strTextF to "美しい日本語" as text
set strTextG to "" as text
set strTextH to "オープンタイプフォント" as text


####NSString
set ocidTextF to objNSString's stringWithString:strTextF
set ocidTextG to objNSString's stringWithString:strTextG
set ocidTextH to objNSString's stringWithString:strTextH

set ocidTextI to ocidTextF's stringByAppendingString:ocidTextG
set ocidTextI to ocidTextI's stringByAppendingString:ocidTextH

log ocidTextI as text
log className() of ocidTextI as text

set ocidTextF to objNSString's stringWithString:strTextF
set ocidTextJ to ocidTextF's stringByAppendingString:ocidTextG
set ocidTextJ to ocidTextJ's stringByAppendingString:""
set ocidTextJ to ocidTextJ's stringByAppendingString:ocidTextH
log ocidTextJ as text
log className() of ocidTextJ as text


####NSMutableString

set strTextF to "美しい日本語" as text
set strTextG to "" as text
set strTextH to "オープンタイプフォント" as text

set ocidMutableString to objNSMutableString's alloc()'s initWithCapacity:0
ocidMutableString's setString:strTextF
ocidMutableString's appendString:strTextG
ocidMutableString's appendString:strTextH
log ocidMutableString as text
log className() of ocidMutableString as text

ocidMutableString's setString:""
ocidMutableString's setString:strTextF
ocidMutableString's appendString:strTextG
ocidMutableString's appendString:""
ocidMutableString's appendString:strTextH
log ocidMutableString as text
log className() of ocidMutableString as text


#########################ログ表示
to doLogView()
tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

より以前の記事一覧

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom