Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

タグ: 10.11savvy

shellの出力結果をスペースでparseする

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:shellの出力結果をスペースでparseする
— Created 2016-03-11 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set a to "3 0x4248387 1920×1200 0 0 -1920 -1200 0 [main]"

set aStr to (current application’s NSString’s stringWithString:a)
–> (NSString) "3 0x4248387 1920×1200 0 0 -1920 -1200 0 [main]"

set aLine to (aStr’s componentsSeparatedByString:" ")
–> (NSArray) {"3", "", "0x4248387", "", "", "", "", "1920×1200", "", "", "", "", "", "0", "", "", "", "", "0", "", "-1920", "", "-1200", "", "", "", "", "", "0", "", "", "", "[main]"}

(aLine’s removeObject:"")
–> (NSArray) {"3", "0x4248387", "1920×1200", "0", "0", "-1920", "-1200", "0", "[main]"}

set bList to aLine as list
–>  {​​​​​"3", ​​​​​"0x4248387", ​​​​​"1920×1200", ​​​​​"0", ​​​​​"0", ​​​​​"-1920", ​​​​​"-1200", ​​​​​"0", ​​​​​"[main]"​​​}

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

&と=で区切られたテキストをrecordに(NSScanner 2)

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:&と=で区切られたテキストをrecordに(NSScanner 2)
— Created 2016-12-12 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aParamStr to "access_token=XXxxx(XXxXXXXXxxXxxXXx))&expires=86399"
set aDict to (parseStrByAmpAndEqual(aParamStr) of me) as record
–>  {​​​​​expires:"86399", ​​​​​access_token:"XXxxx(XXxXXXXXxxXxxXXx))"​​​}

on parseStrByAmpAndEqual(aParamStr)
  set theScanner to current application’s NSScanner’s scannerWithString:aParamStr
  
log (theScanner’s scanLocation())
  
  
set aDict to current application’s NSMutableDictionary’s |dictionary|()
  
repeat
    — terminate check, return the result (aDict) to caller
    
set {theResult, theKey} to theScanner’s scanUpToString:"=" intoString:(reference)
    
if theResult as boolean is false then return aDict
    
log (theScanner’s scanLocation())
    
    
— skip over separator
    
theScanner’s scanString:"=" intoString:(missing value)
    
set {theResult, theValue} to theScanner’s scanUpToString:"&" intoString:(reference)
    
log (theScanner’s scanLocation())
    
    
— skip over separator
    
theScanner’s scanString:"&" intoString:(missing value)
    
    
aDict’s setObject:theValue forKey:theKey
    
log (theScanner’s scanLocation())
  end repeat
end parseStrByAmpAndEqual

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

&と=で区切られたテキストをrecordに 改

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:&と=で区切られたテキストをrecordに 改
— Created 2016-12-12 by Shane Stanley
— Modified 2016-12-14 by edama2
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–http://piyocast.com/as/archives/4358

set aParamStr to "access_token=XXxxx(XXxXXXXXxxXxxXXx))&expires=86399&name="
set aDict to (parseStrByAmpAndEqual(aParamStr) of me)
–>  {expires:"86399", |name|:"", access_token:"XXxxx(XXxXXXXXxxXxxXXx))"}

on parseStrByAmpAndEqual(aParamStr)
  set theScanner to current application’s NSScanner’s scannerWithString:aParamStr
  
set aDict to current application’s NSMutableDictionary’s |dictionary|()
  
  
repeat until (theScanner’s isAtEnd as boolean)
    — terminate check, return the result (aDict) to caller
    
set {theResult, theKey} to theScanner’s scanUpToString:"=" intoString:(reference)
    
    
— skip over separator
    
theScanner’s scanString:"=" intoString:(missing value)
    
set {theResult, theValue} to theScanner’s scanUpToString:"&" intoString:(reference)
    
if theValue is missing value then set theValue to "" –>追加
    
    
— skip over separator
    
theScanner’s scanString:"&" intoString:(missing value)
    
    
aDict’s setObject:theValue forKey:theKey
  end repeat
  
  
return aDict as record
end parseStrByAmpAndEqual

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

テキストの一部を取り出すじっけんASOC

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:テキストの一部を取り出すじっけんASOC
— Created 2017-10-12 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to current application’s NSString’s stringWithString:"0123456789"

set aRes to (aStr’s substringToIndex:1) as string
–>  "0"

set bRes to (aStr’s substringFromIndex:4) as string
–>  "456789"

set cRes to (aStr’s substringWithRange:(current application’s NSMakeRange(3, 6))) as string
–>  "345678"

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

文字列から開始文字列と終了文字列に囲まれた内容を削除

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:文字列から開始文字列と終了文字列に囲まれた内容を削除
— Created 2016-12-12 by Shane Stanley
— Modified 2016-12-14 by edama2
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–http://piyocast.com/as/archives/4358

set aStr to "thru (throughも可)"
set aRes to (trimStrFromTo(aStr, "(", ")") of me)
–>  "thru "

set bStr to "thru "
set bRes to (trimStrFromTo(bStr, "(", ")") of me)
–>  "thru "

on trimStrFromTo(aParamStr, fromStr, toStr)
  set theScanner to current application’s NSScanner’s scannerWithString:aParamStr
  
set anArray to current application’s NSMutableArray’s array()
  
  
repeat until (theScanner’s isAtEnd as boolean)
    — terminate check, return the result (aDict) to caller
    
set {theResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference)
    
    
— skip over separator
    
theScanner’s scanString:fromStr intoString:(missing value)
    
set {theResult, theValue} to theScanner’s scanUpToString:toStr intoString:(reference)
    
if theValue is missing value then set theValue to "" –>追加
    
    
— skip over separator
    
theScanner’s scanString:toStr intoString:(missing value)
    
    
anArray’s addObject:theValue
  end repeat
  
  
if anArray’s |count|() = 0 then return aParamStr
  
  
copy aParamStr to curStr
  
repeat with i in (anArray as list)
    set curStr to repChar(curStr, fromStr & i & toStr, "") of me
  end repeat
  
  
return curStr
end trimStrFromTo

–文字置換
on repChar(aStr, targStr, repStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set bString to aString’s stringByReplacingOccurrencesOfString:targStr withString:repStr
  
set cString to bString as string
  
return cString
end repChar

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

テキストを指定文字でparseする

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:テキストを指定文字でparseする
— Created 2018-02-03 20:21:16 +0900 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set a to current application’s NSString’s stringWithString:"4;1;2008-03-09 18:57:48;2008-03-09 09:57:48;"
set b to a’s componentsSeparatedByString:";"
–>  (NSArray) {​​​​​"4", ​​​​​"1", ​​​​​"2008-03-09 18:57:48", ​​​​​"2008-03-09 09:57:48", ​​​​​""​​​}

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

与えられたテキストからフライト情報を抽出 v2

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:与えられたテキストからフライト情報を抽出 v2
— Created 2015-08-21 by Shane Stanley
— Modified 2015-08-21 by Takaaki Naganoya
— Modified 2015-08-22 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set theString to "UA460 SFO to YVR [Flight] 6/12/2013 United Airlines(UA) #460 dep SFO 7:57pm PDT arr YVR 10:14pm PDT; Ticket #0162360127882, Ticket #0162360127883; conf #K5XBXY; Note:, Seats:—/30A , Seats:—/30B
"

set theDates to (extractTransitInfoFromNaturalText(theString))
–>  {​​​​​{​​​​​​​Flight:"460"​​​​​}​​​}

on extractTransitInfoFromNaturalText(aString)
  set anNSString to current application’s NSString’s stringWithString:aString
  
  
set {theDetector, theError} to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeTransitInformation) |error|:(reference)
  
  
set theMatches to theDetector’s matchesInString:anNSString options:0 range:{0, anNSString’s |length|()}
  
set theResults to theMatches’s valueForKey:"components"
  
  
return theResults as list
end extractTransitInfoFromNaturalText

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

与えられたテキストからdate objectを抽出 v2

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:与えられたテキストからdate objectを抽出 v2
— Created 2015-08-21 by Shane Stanley
— Modified 2015-08-22 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use script "BridgePlus"

set theString to "Sunny September 4
Fri., September 1
Fri. Sept. 2
Fri Sep 3
Sep. 4
Sept. 9
9/8
9/7/15
09/06/2015
9/5/2015
2015/9/5
2015年9月5日
2015年9月5日(土)
2015.9.5
"


set theDates to ASify from (my getDatesIn:theString)
–>  {​​​​​date "2015年9月4日金曜日 12:00:00", ​​​​​date "2015年9月1日火曜日 12:00:00", ​​​​​date "2015年9月2日水曜日 12:00:00", ​​​​​date "2015年9月3日木曜日 12:00:00", ​​​​​date "2015年9月4日金曜日 12:00:00", ​​​​​date "2015年9月9日水曜日 12:00:00", ​​​​​date "2009年7月15日水曜日 12:00:00", ​​​​​date "2015年9月6日日曜日 12:00:00", ​​​​​date "2015年9月5日土曜日 12:00:00", ​​​​​date "2015年9月5日土曜日 12:00:00", ​​​​​date "2015年9月5日土曜日 12:00:00", ​​​​​date "2015年9月5日土曜日 12:00:00"​​​}

on getDatesIn:aString
  set anNSString to current application’s NSString’s stringWithString:aString
  
  set {theDetector, theError} to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeDate) |error|:(reference)
  
  set theMatches to theDetector’s matchesInString:anNSString options:0 range:{0, anNSString’s |length|()}
  
set theResults to theMatches’s valueForKey:"date"
  
  return theResults as list
end getDatesIn:

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

与えられたテキストから電話番号を抽出 v2

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:与えられたテキストから電話番号を抽出 v2
— Created 2015-08-21 by Shane Stanley
— Modified 2015-08-21 by Takaaki Naganoya
— Modified 2015-08-22 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set theString to "長野谷隆昌
(Takaaki Naganoya)
maro@piyocast.com
http://piyocast.com/as
2015年8月21日〜23日
080-1111-2222
東京都練馬区中村橋1-2-3
"

set theDates to (extractPhoneNumberFromNaturalText(theString))
–>  {​​​​​"080-1111-2222"​​​}

on extractPhoneNumberFromNaturalText(aString)
  set anNSString to current application’s NSString’s stringWithString:aString
  
  
set {theDetector, theError} to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypePhoneNumber) |error|:(reference)
  
  
set theMatches to theDetector’s matchesInString:anNSString options:0 range:{0, anNSString’s |length|()}
  
set theResults to theMatches’s valueForKey:"phoneNumber"
  
  
return theResults as list
end extractPhoneNumberFromNaturalText

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

与えられたテキストから住所を抽出 v2

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:与えられたテキストから住所を抽出 v2
— Created 2015-08-21 by Shane Stanley
— Modified 2015-08-21 by Takaaki Naganoya
— Modified 2015-08-22 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set theString to "長野谷隆昌
(Takaaki Naganoya)
maro@piyocast.com
http://piyocast.com/as
2015年8月21日〜23日
東京都練馬区中村橋1-2-3
"

set theDates to (extractAddressFromNaturalText(theString))
–>  {{State:"東京都", Street:"中村橋1-2-3", City:"練馬区"}}

on extractAddressFromNaturalText(aString)
  set anNSString to current application’s NSString’s stringWithString:aString
  
  
set {theDetector, theError} to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeAddress) |error|:(reference)
  
  
set theMatches to theDetector’s matchesInString:anNSString options:0 range:{0, anNSString’s |length|()}
  
set theResults to theMatches’s valueForKey:"addressComponents"
  
  
return theResults as list
end extractAddressFromNaturalText

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

与えられたテキストからリンクURLを抽出 v3

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:与えられたテキストからリンクURLを抽出 v3
— Created 2015-08-21 by Shane Stanley
— Modified 2015-08-21 by Takaaki Naganoya
— Modified 2015-08-22 by Shane Stanley
— Modified 2015-08-23 by Takaaki Naganoya
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set theString to "長野谷隆昌
(Takaaki Naganoya)
maro@piyocast.com
http://piyocast.com/as
2015年8月21日〜23日
080-1111-2222
東京都練馬区中村橋1-2-3
"

set resList to (extractLinksFromNaturalText(theString))
–>  {​​​​​(NSURL) mailto:maro@piyocast.com, ​​​​​(NSURL) http://piyocast.com/as​​​}

set bList to {}
repeat with i in resList
  set the end of bList to (i’s absoluteString()) as text
end repeat
bList
–> {"mailto:maro@piyocast.com", "http://piyocast.com/as"}

on extractLinksFromNaturalText(aString)
  set anNSString to current application’s NSString’s stringWithString:aString
  
  
set {theDetector, theError} to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeLink) |error|:(reference)
  
  
set theMatches to theDetector’s matchesInString:anNSString options:0 range:{0, anNSString’s |length|()}
  
set theResults to theMatches’s valueForKey:"URL"
  
  
return theResults as list
end extractLinksFromNaturalText

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

与えられたテキストからメールアドレスを抽出する

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:与えられたテキストからメールアドレスを抽出する
— Created 2017-01-13 by Shane Stanley
— Modified 2017-01-13 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aText to "
—
Takaaki Naganoya
Piyomaru Software
http://piyocast.com/as
maro@piyocast.com
"

set aRes to findEmailAddressesIn(aText) of me
–>  {​​​​​"maro@piyocast.com"​​​}

on findEmailAddressesIn(someString)
  set theDD to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeLink) |error|:(missing value)
  
set theURLs to theDD’s matchesInString:someString options:0 range:{location:0, |length|:length of someString}
  
set thePredicate to current application’s NSPredicate’s predicateWithFormat:"self.URL.scheme == ’mailto’"
  
set theURLs to theURLs’s filteredArrayUsingPredicate:thePredicate
  
set theURLs to theURLs’s valueForKeyPath:"URL.resourceSpecifier"
  
set theURLs to (current application’s NSSet’s setWithArray:theURLs)’s allObjects()
  
return theURLs as list
end findEmailAddressesIn

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ASOCで文字列を%エンコーディング文字列に変換する

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:ASOCで文字列を%エンコーディング文字列に変換する
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set aString to current application’s NSString’s stringWithString:"The time has come for all good “men”"
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text

–> "The%20time%20has%20come%20for%20all%20good%20%E2%80%9Cmen%E2%80%9D"

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

実体参照している文字列をデコードする(ASOC)

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:実体参照している文字列をデコードする(ASOC)
— Created 2017-01-18 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–http://piyocast.com/as/archives/4568

set aStr to "龍馬伝"
set aRes to decodeCharacterReference(aStr) of me
–>  "龍馬伝"

set aStr to "\"第2エア\""
set aRes to decodeCharacterReference(aStr) of me
–>  "\"第2エア\""

on decodeCharacterReference(aStr)
  set anNSString to current application’s NSString’s stringWithString:aStr
  
set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF16StringEncoding)
  
set styledString to current application’s NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value)
  
set plainText to (styledString’s |string|()) as string
  
return plainText
end decodeCharacterReference

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定文字コードでファイル書き出し(UTF-16LE)v2

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:指定文字コードでファイル書き出し(UTF-16LE)v2
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to "高島屋"
set aPath to choose file name

set cStr to current application’s NSString’s stringWithString:aStr
set thePath to POSIX path of aPath

set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSUTF16LittleEndianStringEncoding) |error|:(missing value)

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定文字コードでファイル書き出し(UTF-16BE)v2

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:指定文字コードでファイル書き出し(UTF-16BE)v2
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to "高島屋"
set aPath to choose file name

set cStr to current application’s NSString’s stringWithString:aStr
set thePath to POSIX path of aPath

set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSUTF16BigEndianStringEncoding) |error|:(missing value)

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定文字コードでファイル書き出し(SJIS)v2

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:指定文字コードでファイル書き出し(SJIS)v2
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to "高島屋"
set aPath to choose file name

set cStr to current application’s NSString’s stringWithString:aStr
set thePath to POSIX path of aPath

set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSShiftJISStringEncoding) |error|:(missing value)

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定文字コードでファイル書き出し(UTF-16)v2

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:指定文字コードでファイル書き出し(UTF-16)v2
— Created 2014-11-11 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to "高島屋"
set aPath to choose file name

set cStr to current application’s NSString’s stringWithString:aStr
set thePath to POSIX path of aPath

set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSUnicodeStringEncoding) |error|:(missing value)

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定文字コードでファイル書き出し(UTF-8)v2

Posted on 2月 6, 2018 by Takaaki Naganoya

AppleScriptの世界観では、文字コードというものは「存在しない」かのように位置付けています。2020年的な状況においてはUTF-8で事足りると言いたいところですが、(日本国内的には)100%そういうわけにも行きません。

AppleScriptのファイル書き出しコマンド(writeコマンド)では、UTF-8だけは文字コード指定のオプションが存在しています。ただし、ほぼ隠し仕様で生AppleEventコードを記述する必要があり、文字列で記述できる「予約語」が設けられているわけではありません。

予約語が存在していないといいつつ、やはり文字コードを指定できないのは不便だったこともあってか、予約語が存在していないものの、特定のファイル書き込み操作を行うと確実にその文字コードでファイル書き込みが行えるという「暗黙の文字コード指定」という手段が存在しています(macOS 10.8でここにバグを作られて大問題になりましたが、10.8.xで修正されました)。

しかし、macOS 10.10以降ではCocoaの機能を手軽に呼び出せるようになったために、状況が一変します。Cocoaの機能さえ呼び出せば、文字コード指定を行ってのファイルの書き出しも読み込みもお手の物。すでに、Cocoaの機能呼び出しのない世界は考えられないほど。なので、Cocoaの機能をAppleScriptネイティブの機能と位置づけ、Cocoaの機能を用いて文字コード指定を柔軟に行えるファイル入出力ルーチンが整備されています。

AppleScriptネイティブのwriteコマンドは、このCocoaの機能に影響を受けています。Cocoaの機能を利用する宣言文、

use framework "Foundation"

がある場合には、writeコマンドをそのまま記述できず(実行時にエラーになる)、tell current application〜end tellのブロック内に記述する必要が出てきます。そのため、掲載のファイル書き出しルーチンのプログラムリスト「UTF8でファイル書き込み」には、一見して無意味と思えるtell current application〜end tellが記述されています。ただ、対処方法がわかっているので使い回しを行うファイル書き出しルーチン側に本記述を書いておくことにしています。

正直なところ、文字コード指定をともなうファイル入出力については、Cocoaの機能を利用した方が手軽なので(AppleScriptのファイル追記モードは便利ですが)Cocoaの機能を使う方法だけ掲載しておけばよいと考えます。ごくまれに、古いバージョンのOS用にScriptを書かなくてはならないとか、Cocoa Scriptingを考慮していない偏屈なアプリケーションのScriptingで必要になることがある、程度のものでしょうか。

AppleScript名:指定文字コードでファイル書き出し(UTF-8)v2
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to "高島屋"
set aPath to choose file name

set cStr to current application’s NSString’s stringWithString:aStr
set thePath to POSIX path of aPath

set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSUTF8StringEncoding) |error|:(missing value)

★Click Here to Open This Script 

AppleScript名:UTF8でファイル書き込み

set dFol to (path to desktop) as string
set fullPath to dFol & "test_utf8.txt"
set aStr to "髙島屋"
set fRes to writeToFileAsUTF8(aStr, fullPath, false) of me

on writeToFileAsUTF8(thisData, targetFile, appendF as boolean)
  tell current application
    try
      set the targetFile to the targetFile as text
      
set the openTargetFile to open for access file targetFile with write permission
      
if appendF is false then set eof of the openTargetFile to 0
      
write thisData to the openTargetFile as «class utf8» starting at eof
      
close access the openTargetFile
      
return true
    on error
      try
        close access file targetFile
      end try
      
return false
    end try
  end tell
end writeToFileAsUTF8

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定文字コードでファイル書き出し(EUC)

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:指定文字コードでファイル書き出し(EUC)
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to "高島屋"
set aPath to choose file name

set cStr to current application’s NSString’s stringWithString:aStr
set thePath to POSIX path of aPath

set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSJapaneseEUCStringEncoding) |error|:(missing value)

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Post navigation

  • Older posts
  • Newer posts

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • macOS 15, Sequoia
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • Numbersで選択範囲のセルの前後の空白を削除
  • メキシカンハットの描画
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AppleScriptによる並列処理
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • macOS 15でも変化したText to Speech環境
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • デフォルトインストールされたフォント名を取得するAppleScript
  • macOS 15 リモートApple Eventsにバグ?
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • AppleScript入門① AppleScriptってなんだろう?
  • macOS 14で変更になったOSバージョン取得APIの返り値
  • Script Debuggerの開発と販売が2025年に終了
  • NSObjectのクラス名を取得 v2.1
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • 有害ではなくなっていたSpaces
  • AVSpeechSynthesizerで読み上げテスト

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (194) 14.0savvy (147) 15.0savvy (135) CotEditor (66) Finder (51) iTunes (19) Keynote (119) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (76) Pages (55) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC