AppleScript TextEdit

TextEdit基本処理


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


##デスクトップ
tell application "Finder"
  set aliasDesktopDirPath to (path to desktop folder from user domain) as alias
end tell
#UNIXパス
set strDesktopDirPath to (POSIX path of aliasDesktopDirPath) as text
#ファイル名
set strFileName to ("デモ.rtf") as text
#デスクトップ+ファイル名
set strSaveFielPath to (strDesktopDirPath & strFileName) as text
#テキスト
set strTextContents to ("AAAAAA\nBBBBBB\nCCCCCC\nDDDDDD") as text
##本処理
tell application "TextEdit"
activate
  ##documentを生成
  set docNewDoc to (make new document with properties {name:strFileName, text:strTextContents, path:strSaveFielPath})
  ##Documentについて
  tell docNewDoc
    ##テキストについて
    tell its text
      ##行毎のテキスト属性
properties of attribute run of every paragraph
      ##文字サイズ
      set its size to 20
      ##フォント
      set its font to "Osaka-Mono"
properties of attribute run of every paragraph
    end tell
    #文字色
    set its color of every paragraph to {0, 0, 0}
  end tell
  #保存
save docNewDoc in (POSIX file strSaveFielPath)
end tell

|

[com.apple.TextEdit]設定変更


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

#!/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
property refMe : a reference to current application


set ocidFalse to (refMe's NSNumber's numberWithBool:false)
set ocidTrue to (refMe's NSNumber's numberWithBool:true)


tell application id "com.apple.TextEdit" to quit

delay 3

##########################################
###【1】ドキュメントのパス
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
##
set ocidPlistFilePathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Containers/com.apple.TextEdit/Data/Library/Preferences/com.apple.TextEdit.plist")
##########################################
### 【2】PLISTを可変レコードとして読み込み
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistFilePathURL)
##########################################
### 【3】処理
(ocidPlistDict's setValue:(ocidTrue) forKey:("UseInlineCSS"))
(ocidPlistDict's setValue:(ocidTrue) forKey:("AddExtensionToNewPlainTextFiles"))
(ocidPlistDict's setValue:(ocidTrue) forKey:("NSNavPanelExpandedStateForSaveMode"))
(ocidPlistDict's setValue:(ocidTrue) forKey:("TextReplacement"))

(ocidPlistDict's setValue:(ocidFalse) forKey:("UseEmbeddedCSS"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("IgnoreHTML"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("IgnoreRichTex"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("SDataDetectors"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("PreserveWhitespace"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("NSNavLastUserSetHideExtensionButtonState"))

(ocidPlistDict's setValue:(ocidFalse) forKey:("SmartSubstitutionsEnabledInRichTextOnly"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("SmartQuotes"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("SmartCopyPaste"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("SmartDashes"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("SSmartLinks"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("NSAutomaticPeriodSubstitutionEnabled"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("NSAutomaticTextCompletionEnabled"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("CheckSpellingAsYouTypeEnabledInRichTextOnly"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("NSNavLastUserSetHideExtensionButtonState"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("NSDocumentSuppressTempVersionStoreWarning"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("NSAutomaticDashSubstitutionEnabled"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("NSAutomaticCapitalizationEnabled"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("SCheckGrammarWithSpelling"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("CorrectSpellingAutomatically"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("CheckSpellingWhileTyping"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("NSAutomaticSpellingCorrectionEnabled"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("NSAutomaticTextCompletionCollapsed"))
(ocidPlistDict's setValue:(ocidFalse) forKey:("NSAutomaticQuoteSubstitutionEnabled"))

(ocidPlistDict's setValue:("Osaka-Mono") forKey:("NSFixedPitchFont"))
(ocidPlistDict's setValue:("16") forKey:("NSFixedPitchFontSize"))
(ocidPlistDict's setValue:("Osaka-Mono") forKey:("NSFont"))
(ocidPlistDict's setValue:("16") forKey:("NSFontSize"))

(ocidPlistDict's setValue:(4) forKey:("PlainTextEncoding"))
(*
NSASCIIStringEncoding 1
NSNEXTSTEPStringEncoding 2
NSJapaneseEUCStringEncoding 3
NSUTF8StringEncoding 4
NSISOLatin1StringEncoding 5
NSSymbolStringEncoding 6
NSNonLossyASCIIStringEncoding 7
NSShiftJISStringEncoding 8
NSISOLatin2StringEncoding 9
NSUnicodeStringEncoding 10
NSWindowsCP1251StringEncoding 11
NSWindowsCP1252StringEncoding 12
NSWindowsCP1253StringEncoding 13
NSWindowsCP1254StringEncoding 14
NSWindowsCP1250StringEncoding 15
NSISO2022JPStringEncoding 21
NSMacOSRomanStringEncoding 30
NSUTF16StringEncoding 10
NSUTF16BigEndianStringEncoding 2415919360
NSUTF16LittleEndianStringEncoding 2483028224
NSUTF32StringEncoding 2348810496
NSUTF32BigEndianStringEncoding 2550137088
NSUTF32LittleEndianStringEncoding 2617245952
NSProprietaryStringEncoding 65536
*)


##########################################
####【4】保存 ここは上書き
set boolDone to ocidPlistDict's writeToURL:(ocidPlistFilePathURL) atomically:true
log boolDone
if boolDone = true then
return "正常終了"
  #####CFPreferencesを再起動
  set strCommandText to "/usr/bin/killall cfprefsd" as text
  do shell script strCommandText
else
return "保存に失敗しました"
end if




return







|

テキストエディタ 初期設定(自分用)

老眼対策で、文字サイズを16ptにした

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

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


do shell script "/usr/bin/defaults write com.apple.TextEdit UseInlineCSS -boolean TRUE"
do shell script "/usr/bin/defaults write com.apple.TextEdit UseEmbeddedCSS -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit IgnoreHTML -boolean FALSE"

do shell script "/usr/bin/defaults write com.apple.TextEdit NSFixedPitchFont -string \"Osaka-Mono\""
do shell script "/usr/bin/defaults write com.apple.TextEdit NSFixedPitchFontSize -string \"16\""

do shell script "/usr/bin/defaults write com.apple.TextEdit NSFont -string \"Osaka-Mono\""
do shell script "/usr/bin/defaults write com.apple.TextEdit NSFontSize -string \"16\""


do shell script "/usr/bin/defaults write com.apple.TextEdit IgnoreRichTex -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit AddExtensionToNewPlainTextFiles -boolean TRUE"
do shell script "/usr/bin/defaults write com.apple.TextEdit ShowRuler -boolean TRUE"


do shell script "/usr/bin/defaults write com.apple.TextEdit SDataDetectors -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit PreserveWhitespace -boolean FALSE"

do shell script "/usr/bin/defaults write com.apple.TextEdit SmartSubstitutionsEnabledInRichTextOnly -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit SmartQuotes -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit SmartCopyPaste -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit SmartDashes -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit SSmartLinks -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit NSAutomaticPeriodSubstitutionEnabled -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit NSAutomaticTextCompletionEnabled -boolean FALSE"

do shell script "/usr/bin/defaults write com.apple.TextEdit CheckSpellingAsYouTypeEnabledInRichTextOnly -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit NSNavLastUserSetHideExtensionButtonState -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit NSDocumentSuppressTempVersionStoreWarning -boolean FALSE"

do shell script "/usr/bin/defaults write com.apple.TextEdit NSAutomaticDashSubstitutionEnabled -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit NSAutomaticCapitalizationEnabled -boolean FALSE"

do shell script "/usr/bin/defaults write com.apple.TextEdit SCheckGrammarWithSpelling -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit CorrectSpellingAutomatically -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit CheckSpellingWhileTyping -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit NSAutomaticSpellingCorrectionEnabled -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit NSAutomaticTextCompletionCollapsed -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit NSAutomaticQuoteSubstitutionEnabled -boolean FALSE"

do shell script "/usr/bin/defaults write com.apple.TextEdit NSNavPanelExpandedStateForSaveMode -boolean TRUE"
do shell script "/usr/bin/defaults write com.apple.TextEdit NSNavLastUserSetHideExtensionButtonState -boolean FALSE"
do shell script "/usr/bin/defaults write com.apple.TextEdit TextReplacement -boolean TRUE"


do shell script "/usr/bin/defaults write com.apple.TextEdit PlainTextEncoding -integer 4"
(*
NSASCIIStringEncoding 1
NSNEXTSTEPStringEncoding 2
NSJapaneseEUCStringEncoding 3
NSUTF8StringEncoding 4
NSISOLatin1StringEncoding 5
NSSymbolStringEncoding 6
NSNonLossyASCIIStringEncoding 7
NSShiftJISStringEncoding 8
NSISOLatin2StringEncoding 9
NSUnicodeStringEncoding 10
NSWindowsCP1251StringEncoding 11
NSWindowsCP1252StringEncoding 12
NSWindowsCP1253StringEncoding 13
NSWindowsCP1254StringEncoding 14
NSWindowsCP1250StringEncoding 15
NSISO2022JPStringEncoding 21
NSMacOSRomanStringEncoding 30
NSUTF16StringEncoding 10
NSUTF16BigEndianStringEncoding 2415919360
NSUTF16LittleEndianStringEncoding 2483028224
NSUTF32StringEncoding 2348810496
NSUTF32BigEndianStringEncoding 2550137088
NSUTF32LittleEndianStringEncoding 2617245952
NSProprietaryStringEncoding 65536
詳しくはこちら
https://quicktimer.cocolog-nifty.com/icefloe/2023/05/post-0d74ca.html
*)

#####CFPreferencesを再起動
set strCommandText to "/usr/bin/killall cfprefsd" as text
do shell script strCommandText


|

[NSStringEncoding]PlainTextEncodingの番号リスト

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

#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe
########################


/usr/bin/defaults write com.apple.TextEdit PlainTextEncoding -integer 4




4のことね

PlainTextEncoding番号一覧
NSStringEncoding番号
NSASCIIStringEncoding1
NSNEXTSTEPStringEncoding2
NSJapaneseEUCStringEncoding3
NSUTF8StringEncoding4
NSISOLatin1StringEncoding5
NSSymbolStringEncoding6
NSNonLossyASCIIStringEncoding7
NSShiftJISStringEncoding8
NSISOLatin2StringEncoding9
NSUnicodeStringEncoding10
NSWindowsCP1251StringEncoding11
NSWindowsCP1252StringEncoding12
NSWindowsCP1253StringEncoding13
NSWindowsCP1254StringEncoding14
NSWindowsCP1250StringEncoding15
NSISO2022JPStringEncoding21
NSMacOSRomanStringEncoding30
NSUTF16StringEncoding10
NSUTF16BigEndianStringEncoding2415919360
NSUTF16LittleEndianStringEncoding2483028224
NSUTF32StringEncoding2348810496
NSUTF32BigEndianStringEncoding2550137088
NSUTF32LittleEndianStringEncoding2617245952
NSProprietaryStringEncoding65536


取得は


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

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

log current application's NSASCIIStringEncoding as text
log current application's NSNEXTSTEPStringEncoding as text
log current application's NSJapaneseEUCStringEncoding as text
log current application's NSUTF8StringEncoding as text
log current application's NSISOLatin1StringEncoding as text
log current application's NSSymbolStringEncoding as text
log current application's NSNonLossyASCIIStringEncoding as text
log current application's NSShiftJISStringEncoding as text
log current application's NSISOLatin2StringEncoding as text
log current application's NSUnicodeStringEncoding as text
log current application's NSWindowsCP1251StringEncoding as text
log current application's NSWindowsCP1252StringEncoding as text
log current application's NSWindowsCP1253StringEncoding as text
log current application's NSWindowsCP1254StringEncoding as text
log current application's NSWindowsCP1250StringEncoding as text
log current application's NSISO2022JPStringEncoding as text
log current application's NSMacOSRomanStringEncoding as text
log current application's NSUTF16StringEncoding as text
log current application's NSUTF16BigEndianStringEncoding as text
log current application's NSUTF16LittleEndianStringEncoding as text
log current application's NSUTF32StringEncoding as text
log current application's NSUTF32BigEndianStringEncoding as text
log current application's NSUTF32LittleEndianStringEncoding as text
log current application's NSProprietaryStringEncoding as text

|

HTMLで保存ソース表示用のTEXTファイルも生成する

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

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

property refMe : a reference to current application

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

###ソーステキストで開く
tell application "TextEdit"
  set numCntWindow to count of every window
  if numCntWindow = 0 then
return "ウィンドウがありません"
  end if
  tell front document
    set strFilePath to path as text
save
  end tell
end tell
###パスをNSStringに
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
##NSPathStoreでパス確定
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
###コンテナ
set ocidContainerDirPath to ocidFilePath's stringByDeletingLastPathComponent()
##ファイル名
set ocidFileName to ocidFilePath's lastPathComponent()
##拡張子
set ocidExtensionName to ocidFileName's pathExtension()
##ベースパス
set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
##保存用のファイル名を定義
set ocidHtmlFileName to ocidBaseFileName's stringByAppendingPathExtension:("html")
set ocidTextFileName to ocidHtmlFileName's stringByAppendingPathExtension:("txt")
##保存用のパスを定義
set ocidHtmlFilePath to ocidContainerDirPath's stringByAppendingPathComponent:(ocidHtmlFileName)
set ocidTextFilePath to ocidContainerDirPath's stringByAppendingPathComponent:(ocidTextFileName)
###エイリアスfurlにしておく
tell application "Finder"
  set aliasHtmlFilePath to (POSIX file (ocidHtmlFilePath as text)) as «class furl»
  set aliasTextFilePath to (POSIX file (ocidTextFilePath as text)) as «class furl»
end tell

###HTMLで保存
tell application "TextEdit"
  tell front document
    set strFilePath to path as text
save in (aliasHtmlFilePath as «class furl»)
  end tell
end tell
###ソース閲覧用にテキスト拡張子も作成
appFileManager's copyItemAtPath:(ocidHtmlFilePath) toPath:(ocidTextFilePath) |error|:(reference)

###ソーステキストで開く
tell application "TextEdit"
  open (aliasTextFilePath as alias)
end tell

|

[TextEdit]新規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 scripting additions

property refMe : a reference to current application

set strHtmlFileName to ("名称未設定.html") as text

set appFileManager to refMe's NSFileManager's defaultManager()
set ocidHomeDirPathURL to appFileManager's homeDirectoryForCurrentUser
set ocidDesktopDirPathURL to ocidHomeDirPathURL's URLByAppendingPathComponent:"Desktop" isDirectory:true
set ocidFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:strHtmlFileName isDirectory:false
set strFilePathURL to ocidFilePathURL's |path|() as text
tell application "Finder"
  set aliasFilePathURL to ocidFilePathURL as «class furl»
end tell

###ソーステキストで開く
tell application "TextEdit"
  make new document with properties {name:strHtmlFileName, path:strFilePathURL}
  tell front document
    activate
save in aliasFilePathURL
  end tell
end tell


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

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

property refMe : a reference to current application

set strHtmlFileName to ("名称未設定.html") as text


tell application "Finder"
  set aliasDesktopPath to path to desktop folder from user domain as alias
  set strDesktopFilePath to POSIX path of aliasDesktopPath as text
  set aliasFilePathURL to POSIX file (strDesktopFilePath & strHtmlFileName) as «class furl»
  set strFilePath to POSIX path of aliasFilePathURL as text
end tell

###ソーステキストで開く
tell application "TextEdit"
  make new document with properties {name:strHtmlFileName, path:strFilePath}
  tell front document
    activate
save in aliasFilePathURL
  end tell
end tell

|

[TextEdit]新規プレーンテキストを作成する

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


set strFileName to "サンプル.txt" as text

set strSetText to "美しい日本語"

tell application "Finder"
  set aliasPathToMe to path to me as alias
  set aliasContainerPath to container of aliasPathToMe as alias
end tell

tell application "Finder"
  try
    make new file at aliasContainerPath with properties {name:(strFileName)}
  end try
end tell

tell application "Finder"
  set aliasFilePath to (file strFileName of folder aliasContainerPath) as «class furl»
end tell

tell application "TextEdit"
  activate
  set objAvtivDoc to open file aliasFilePath
  tell objAvtivDoc
    activate
    set its text to strSetText
    save in aliasFilePath
  end tell
end tell

|

[NSUTF8StringEncoding]新規ブランクRTF リッチテキストを作成する。

本当はNSRTFTextDocumentTypeを使うんだろうなあ


#!/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 objFileManager to refMe's NSFileManager's defaultManager()


###ファイル名

set strFineName to "名称未設定.rtf"


###デスクトップフォルダ

set listResponse to (objFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))

set ocidUserDesktopPathURL to (item 1 of listResponse)

###ファイルパスURL

set ocidSaveFilePathURL to ocidUserDesktopPathURL's URLByAppendingPathComponent:strFineName isDirectory:false

###ダミーのRTFのソース

set strText to "{\\rtf1\\ansi\\deff0}" as text

###Stringsにして

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

####デスクトップに保存

set boolDone to ocidText's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)


|

[ascending]テキスト並び替え

昇順

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

property refNSMutableArray : a reference to refMe's NSMutableArray



tell application "TextEdit"

  tell front window

    tell front document

      set strContents to its text

      properties

    end tell

  end tell

end tell




#######選択テキストを格納

set ocidText to refNSString's stringWithString:strContents

#######可変Arrayを初期化して準備

set ocidReadData to refNSMutableArray's alloc()'s initWithCapacity:0

#######改行を指定

set ocidNewlineCharacterSett to refMe's NSCharacterSet's newlineCharacterSet()

#######改行で区切りでArrayに格納

set ocidReadData to ocidText's componentsSeparatedByCharactersInSet:ocidNewlineCharacterSett

#######並び替え

###逆順に処理するのでdecendig=降順に並び替える

set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:true selector:"localizedStandardCompare:"

###並べ替え

set ocidSortedArray to ocidReadData's sortedArrayUsingDescriptors:{ocidDescriptor}

###改行入れてテキスト化

set ocidSortedString to ocidSortedArray's componentsJoinedByString:"\n"

####テキスト化

set strSortedContants to ocidSortedString as text




####テキストエディターに内容を戻す

tell application "TextEdit"

  tell front window

    tell front document

      set its text to strSortedContants

      

    end tell

  end tell

end tell



降順

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

property refNSMutableArray : a reference to refMe's NSMutableArray



tell application "TextEdit"

  tell front window

    tell front document

      set strContents to its text

      properties

    end tell

  end tell

end tell




#######選択テキストを格納

set ocidText to refNSString's stringWithString:strContents

#######可変Arrayを初期化して準備

set ocidReadData to refNSMutableArray's alloc()'s initWithCapacity:0

#######改行を指定

set ocidNewlineCharacterSett to refMe's NSCharacterSet's newlineCharacterSet()

#######改行で区切りでArrayに格納

set ocidReadData to ocidText's componentsSeparatedByCharactersInSet:ocidNewlineCharacterSett

#######並び替え

###逆順に処理するのでdecendig=降順に並び替える

set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:false selector:"localizedStandardCompare:"

###並べ替え

set ocidSortedArray to ocidReadData's sortedArrayUsingDescriptors:{ocidDescriptor}

###改行入れてテキスト化

set ocidSortedString to ocidSortedArray's componentsJoinedByString:"\n"

####テキスト化

set strSortedContants to ocidSortedString as text




####テキストエディターに内容を戻す

tell application "TextEdit"

  tell front window

    tell front document

      set its text to strSortedContants

      

    end tell

  end tell

end tell



|

[TextEdit]ファイルを保存する(書き出し)

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

property refNSURL : a reference to refMe's NSURL

property refNSMutableArray : a reference to refMe's NSMutableArray


property refNSWorkspace : a reference to refMe's NSWorkspace



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

####前面の書類から必要な情報を取得

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

tell application "TextEdit"

  activate

  tell front document

    set strFileName to name as text

    set strFilePath to path as text

  end tell

end tell


if strFilePath is "" then

  log "このファイルは未保存なのでデスクトップに保存します"

  set strFilePath to ("~/Desktop/" & strFileName) as text

end if



####ファイル名から拡張子取ってベースファイル名

set ocidFileName to refNSString's stringWithString:strFileName

set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()


###パスからファイル名を取って保存先パスを調べる

set ocidFilePathStr to refNSString's stringWithString:strFilePath

set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()

###コンテナフォルダ(保存先のフォルダパス)

set ocidContainerDirPath to ocidFilePath's stringByDeletingLastPathComponent()



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

####ダイアログ

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

set listChooser to {"Doc\tcom.microsoft.word.doc", "XML\tcom.microsoft.word.wordml", "DOCX\torg.openxmlformats.wordprocessingml.document", "ODT\torg.oasis-open.opendocument.text", "HTML\tpublic.html", "WEBARCHIVE\tcom.apple.webarchive", "RTFD\tcom.apple.rtfd", "RTF\tpublic.rtf"} as list

try

  tell current application

    activate

    set listResponse to (choose from list listChooser with title "保存形式を選んでください" with prompt "保存形式を選んでください" default items (item 1 of listChooser) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed) as list

  end tell

on error

  log "エラーしました"

  return "エラーしました"

  error "エラーしました" number -200

end try

####multiple selections allowed falseの場合

if listResponse is false then

  return "キャンセルしましたA"

  error "キャンセルしました" number -200

  ####multiple selections allowed

else if (item 1 of listResponse) is false then

  return "キャンセルしましたB"

  error "キャンセルしました" number -200

end if



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

####本処理

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

repeat with itemResponse in listResponse

  ###戻り値をタブでリストにする

  set ocidItemTextArray to (refNSMutableArray's alloc()'s initWithCapacity:0)

  set ocidDelimiters to ((refMe's NSCharacterSet)'s characterSetWithCharactersInString:"\t")

  set ocidItemResponse to (refNSString's stringWithString:itemResponse)

  set ocidItemTextArray to (ocidItemResponse's componentsSeparatedByCharactersInSet:ocidDelimiters)

  ###リストの1つ目が拡張子

  set ocidFirstObject to ocidItemTextArray's firstObject()

  ###小文字に

  set ocidExetensionName to ocidFirstObject's lowercaseString()

  set strExetensionName to ocidExetensionName as text

  #####保存用のファイル名を整形

  set ocidNewFileName to (ocidBaseFileName's stringByAppendingPathExtension:strExetensionName)

  ####コンテナにファイル名追加して保存先パス

  set ocidFilePath to (ocidContainerDirPath's stringByAppendingPathComponent:ocidNewFileName)

  ###テキスト形式に戻す

  set strSaveFilePath to ocidFilePath as text

  tell application "TextEdit"

    activate

    tell front document

      activate

      save in (POSIX file strSaveFilePath)

    end tell

  end tell

  

end repeat



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

####処理終了

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

###コンテナディレクトリ(保存先)をNSURLにして

set ocidFilePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidContainerDirPath isDirectory:true

###Finderで開く

set ocidShardWorkspace to refNSWorkspace's sharedWorkspace()

ocidShardWorkspace's openURL:ocidFilePathURL

tell application "Finder" to activate



return

|

その他のカテゴリー

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