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

指定ファイルをFinderで選択表示_asoc

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:指定ファイルをFinderで選択表示_asoc
— Created 2016-10-31 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aFile to POSIX path of (choose file)
set pathStr to current application’s NSString’s stringWithString:aFile
set parentPath to pathStr’s stringByDeletingLastPathComponent()
set aRes to current application’s NSWorkspace’s sharedWorkspace()’s selectFile:pathStr inFileViewerRootedAtPath:parentPath

★Click Here to Open This Script 

Posted in file System | Tagged 10.11savvy 10.12savvy 10.13savvy Finder | Leave a comment

Appleのハードウェアのアイコン名を一覧から選択して返す

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:Appleのハードウェアのアイコン名を一覧から選択して返す
— Created 2017-07-29 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aRes to chooseHardwareModel() of me

on chooseHardwareModel()
  set sRes to (do shell script "ls " & (quoted form of "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/") & "com.apple.*.icns")
  
set aList to paragraphs of sRes
  
set aaList to choose from list aList
  
if aaList = false then return
  
set aPath to first item of aaList
  
set aStr to ((current application’s NSString’s stringWithString:aPath)’s lastPathComponent()’s stringByDeletingPathExtension()) as string
  
return aStr
end chooseHardwareModel

★Click Here to Open This Script 

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

shell環境変数を取得する

Posted on 2月 24, 2018 by Takaaki Naganoya

do shell scriptコマンドでシェルコマンドが実行される場合の環境変数の確認は、1コマンドで実行できます。Terminal.app上で実行するときと初期条件が異なるので、必要に応じて環境変数の設定が必要です。

do shell script "env"

★Click Here to Open This Script 

これが、Cocoaの機能だとどういう条件で実行されるのか、というのがこのScriptを書いた原因です。動かしてみて、「ああ、do shell scriptコマンドと同じなんだね」ということが理解できました。

AppleScript名:shell環境変数を取得する
— Created 2016-03-16 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

current application’s NSProcessInfo’s processInfo()’s environment()
–>  (NSDictionary) {​​​​​PATH:"/usr/bin:/bin:/usr/sbin:/sbin", ​​​​​TMPDIR:"/var/folders/h4/jfhlwst88xl9z0001s7k9vk00000gr/T/", ​​​​​LOGNAME:"maro", ​​​​​XPC_FLAGS:"0x0", ​​​​​HOME:"/Users/me", ​​​​​Apple_PubSub_Socket_Render:"/private/tmp/com.apple.launchd.KvufhRIpUw/Render", ​​​​​USER:"me", ​​​​​SSH_AUTH_SOCK:"/private/tmp/com.apple.launchd.3B8HzKhUz5/Listeners", ​​​​​DISPLAY:"/private/tmp/com.apple.launchd.el9lFx0WpV/org.macosforge.xquartz:0", ​​​​​XPC_SERVICE_NAME:"au.com.myriad-com.ASObjC-Explorer-4.1891872", ​​​​​SHELL:"/bin/bash", ​​​​​__CF_USER_TEXT_ENCODING:"0x1F8:0x1:0xE"​​​}

★Click Here to Open This Script 

Posted in shell script System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ベクトルの長さを求める

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:ベクトルの長さを求める
set vL to getVectorLength(5, 2) of me

–ベクトルの長さを求める
on getVectorLength(a, b)
  return getSQR(a ^ 2 + b ^ 2) of me
end getVectorLength

–平方根を求める
on getSQR(aNum)
  return (aNum ^ 0.5)
end getSQR

★Click Here to Open This Script 

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

MatRによる行列計算テスト v4

Posted on 2月 24, 2018 by Takaaki Naganoya

–> DCMatrix.framework

–> download sample data ”matrix.txt”

AppleScript名:MatRによる行列計算テスト v4
— Created 2017-05-15 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "DCMatrix" –https://github.com/davidcox95/MatR

–タブ区切りテキストファイルからMatrixを作る
set aPosixFile to POSIX path of (choose file)
set aMatrix to current application’s |Matrix|’s alloc()’s initWithContentsFromFile:aPosixFile
aMatrix’s |print|()
(*
5 6 7 8
3 4 5 6
1 2 3 4
*)

★Click Here to Open This Script 

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

MatRによる行列計算テスト v3

Posted on 2月 24, 2018 by Takaaki Naganoya

–> DCMatrix.framework

AppleScript名:MatRによる行列計算テスト v3
— Created 2017-05-15 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "DCMatrix" –https://github.com/davidcox95/MatR

–listからMatrixを作る
set mList to {{1, 2, 3, 4}, {11, 12, 13, 14}, {21, 22, 23, 24}}
set aMatrix to current application’s |Matrix|’s alloc()’s initWithArray:mList andRows:3 byColumns:4
aMatrix’s |print|()
(*
1 2 3 4
11 12 13 14
21 22 23 24
*)

set a1Res to aMatrix’s |transpose|()
(
a1Res’s |print|())
(*
1 11 21
2 12 22
3 13 23
4 14 24
*)

★Click Here to Open This Script 

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

MatRによる行列計算テスト v2

Posted on 2月 24, 2018 by Takaaki Naganoya

–> DCMatrix.framework

AppleScript名:MatRによる行列計算テスト v2
— Created 2017-05-15 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "DCMatrix" –https://github.com/davidcox95/MatR

–行列データをdoubleのListに
set aMatrix to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:5) andRows:3 byColumns:4
set aList to doubleListFromMatrix(aMatrix) of me
–>  {​​​​​{​​​​​​​5.0, ​​​​​​​5.0, ​​​​​​​5.0, ​​​​​​​5.0​​​​​}, ​​​​​{​​​​​​​5.0, ​​​​​​​5.0, ​​​​​​​5.0, ​​​​​​​5.0​​​​​}, ​​​​​{​​​​​​​5.0, ​​​​​​​5.0, ​​​​​​​5.0, ​​​​​​​5.0​​​​​}​​​}

–行列データをintegerのListに
set bList to intListFromMatrix(aMatrix) of me
–>  {​​​​​{​​​​​​​5, ​​​​​​​5, ​​​​​​​5, ​​​​​​​5​​​​​}, ​​​​​{​​​​​​​5, ​​​​​​​5, ​​​​​​​5, ​​​​​​​5​​​​​}, ​​​​​{​​​​​​​5, ​​​​​​​5, ​​​​​​​5, ​​​​​​​5​​​​​}​​​}

–行列データをstringのListに
set cList to strListFromMatrix(aMatrix) of me
–>  {​​​​​{​​​​​​​"5", ​​​​​​​"5", ​​​​​​​"5", ​​​​​​​"5"​​​​​}, ​​​​​{​​​​​​​"5", ​​​​​​​"5", ​​​​​​​"5", ​​​​​​​"5"​​​​​}, ​​​​​{​​​​​​​"5", ​​​​​​​"5", ​​​​​​​"5", ​​​​​​​"5"​​​​​}​​​}

set bMatrix to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:3) andRows:4 byColumns:3
set bScalar to current application’s NSNumber’s numberWithDouble:2
set bRes to bMatrix’s addScalar:bScalar
set bbRes to intListFromMatrix(bRes) of me
–>  {​​​​​{​​​​​​​5, ​​​​​​​5, ​​​​​​​5​​​​​}, ​​​​​{​​​​​​​5, ​​​​​​​5, ​​​​​​​5​​​​​}, ​​​​​{​​​​​​​5, ​​​​​​​5, ​​​​​​​5​​​​​}, ​​​​​{​​​​​​​5, ​​​​​​​5, ​​​​​​​5​​​​​}​​​}

set cScalar to current application’s NSNumber’s numberWithDouble:2
set cRes to bMatrix’s subtractScalar:cScalar
set ccRes to intListFromMatrix(cRes) of me
–>  {​​​​​{​​​​​​​1, ​​​​​​​1, ​​​​​​​1​​​​​}, ​​​​​{​​​​​​​1, ​​​​​​​1, ​​​​​​​1​​​​​}, ​​​​​{​​​​​​​1, ​​​​​​​1, ​​​​​​​1​​​​​}, ​​​​​{​​​​​​​1, ​​​​​​​1, ​​​​​​​1​​​​​}​​​}

set dScalar to current application’s NSNumber’s numberWithDouble:2
set eRes to bMatrix’s multiplyScalar:dScalar
set eeRes to intListFromMatrix(eRes) of me
–>  {​​​​​{​​​​​​​6, ​​​​​​​6, ​​​​​​​6​​​​​}, ​​​​​{​​​​​​​6, ​​​​​​​6, ​​​​​​​6​​​​​}, ​​​​​{​​​​​​​6, ​​​​​​​6, ​​​​​​​6​​​​​}, ​​​​​{​​​​​​​6, ​​​​​​​6, ​​​​​​​6​​​​​}​​​}

set fScalar to current application’s NSNumber’s numberWithDouble:2
set fRes to bMatrix’s divideScalar:fScalar
set ffRes to doubleListFromMatrix(fRes) of me
–>  {​​​​​{​​​​​​​1.5, ​​​​​​​1.5, ​​​​​​​1.5​​​​​}, ​​​​​{​​​​​​​1.5, ​​​​​​​1.5, ​​​​​​​1.5​​​​​}, ​​​​​{​​​​​​​1.5, ​​​​​​​1.5, ​​​​​​​1.5​​​​​}, ​​​​​{​​​​​​​1.5, ​​​​​​​1.5, ​​​​​​​1.5​​​​​}​​​}

set m4 to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:4) andRows:3 byColumns:3
set m2 to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:2) andRows:3 byColumns:3
set gRes to m4’s addMatrix:m2
set ggRes to intListFromMatrix(gRes) of me
–>  {​​​​​{​​​​​​​6, ​​​​​​​6, ​​​​​​​6​​​​​}, ​​​​​{​​​​​​​6, ​​​​​​​6, ​​​​​​​6​​​​​}, ​​​​​{​​​​​​​6, ​​​​​​​6, ​​​​​​​6​​​​​}​​​}

set hRes to m4’s subtractMatrix:m2
set hhRes to intListFromMatrix(hRes) of me
–>  {​​​​​{​​​​​​​2, ​​​​​​​2, ​​​​​​​2​​​​​}, ​​​​​{​​​​​​​2, ​​​​​​​2, ​​​​​​​2​​​​​}, ​​​​​{​​​​​​​2, ​​​​​​​2, ​​​​​​​2​​​​​}​​​}

set iRes to m4’s multiplyMatrix:m2
set iiRes to intListFromMatrix(iRes) of me
–>  {​​​​​{​​​​​​​24, ​​​​​​​24, ​​​​​​​24​​​​​}, ​​​​​{​​​​​​​24, ​​​​​​​24, ​​​​​​​24​​​​​}, ​​​​​{​​​​​​​24, ​​​​​​​24, ​​​​​​​24​​​​​

on doubleListFromMatrix(aMatrix)
  set aList to {}
  
set aRows to aMatrix’s rows()
  
set aColumns to aMatrix’s columns()
  
  
repeat with rowC from 0 to (aRows – 1)
    set colList to {}
    
repeat with colC from 0 to (aColumns – 1)
      set the end of colList to (aMatrix’s getElementAtRow:rowC andColumn:colC) as real
    end repeat
    
set the end of aList to colList
  end repeat
  
return aList
end doubleListFromMatrix

on intListFromMatrix(aMatrix)
  set aList to {}
  
set aRows to aMatrix’s rows()
  
set aColumns to aMatrix’s columns()
  
  
repeat with rowC from 0 to (aRows – 1)
    set colList to {}
    
repeat with colC from 0 to (aColumns – 1)
      set the end of colList to (aMatrix’s getElementAtRow:rowC andColumn:colC) as integer
    end repeat
    
set the end of aList to colList
  end repeat
  
return aList
end intListFromMatrix

on strListFromMatrix(aMatrix)
  set aList to {}
  
set aRows to aMatrix’s rows()
  
set aColumns to aMatrix’s columns()
  
  
repeat with rowC from 0 to (aRows – 1)
    set colList to {}
    
repeat with colC from 0 to (aColumns – 1)
      set the end of colList to (aMatrix’s getElementAtRow:rowC andColumn:colC) as string
    end repeat
    
set the end of aList to colList
  end repeat
  
return aList
end strListFromMatrix

★Click Here to Open This Script 

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

MatRによる行列計算テスト v1

Posted on 2月 24, 2018 by Takaaki Naganoya

–> DCMatrix.framework

AppleScript名:MatRによる行列計算テスト v1
— Created 2017-05-15 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "DCMatrix" –https://github.com/davidcox95/MatR

–指定数値、指定サイズ(行、列)で行列データを作成する
set aMatrix to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:5) andRows:3 byColumns:4
(
aMatrix’s |print|())
(*
5 5 5 5
5 5 5 5
5 5 5 5
*)

–行列データに数値を加算する
set bMatrix to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:7) andRows:4 byColumns:3
set bScalar to current application’s NSNumber’s numberWithDouble:2
set bRes to bMatrix’s addScalar:bScalar
bRes’s |print|()
(*
9 9 9
9 9 9
9 9 9
9 9 9
*)

–行列データから数値を減算する
set cScalar to current application’s NSNumber’s numberWithDouble:3
set cRes to bMatrix’s subtractScalar:cScalar
cRes’s |print|()
(*
4 4 4
4 4 4
4 4 4
4 4 4
*)

–行列データに数値を乗算する
set dScalar to current application’s NSNumber’s numberWithDouble:2
set eRes to bMatrix’s multiplyScalar:dScalar
eRes’s |print|()
(*
14 14 14
14 14 14
14 14 14
14 14 14
*)

–行列データに数値を除算する
set fScalar to current application’s NSNumber’s numberWithDouble:2
set fRes to bMatrix’s divideScalar:fScalar
fRes’s |print|()
(*
3.5 3.5 3.5
3.5 3.5 3.5
3.5 3.5 3.5
3.5 3.5 3.5
*)

–行列同士の加算
set m4 to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:4) andRows:3 byColumns:3
set m2 to current application’s |Matrix|’s alloc()’s initWithValue:(current application’s NSNumber’s numberWithDouble:2) andRows:3 byColumns:3
set gRes to m4’s addMatrix:m2
gRes’s |print|()
(*
6 6 6
6 6 6
6 6 6
*)

–行列同士の減算
set hRes to m4’s subtractMatrix:m2
hRes’s |print|()
(*
2 2 2
2 2 2
2 2 2
*)

–行列同士の乗算
set iRes to m4’s multiplyMatrix:m2
iRes’s |print|()
(*
24 24 24
24 24 24
24 24 24
*)

★Click Here to Open This Script 

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

与えられたデータのうちIPアドレスとして妥当なもののみを抽出

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:与えられたデータのうちIPアドレスとして妥当なもののみを抽出
— Created 2018-01-03 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property NSPredicate : a reference to current application’s NSPredicate
property NSArray : a reference to current application’s NSArray

set aaList to {"112.4.208.194", "17.20.30..12"}
set aResList to {}
repeat with i in aaList
  set j to contents of i
  
set the end of aResList to chkIPAddressFormat(j) of me
end repeat
aResList

on chkIPAddressFormat(aStr as string)
  set aList to {aStr}
  
set anArray to NSArray’s arrayWithArray:aList
  
set aPred to NSPredicate’s predicateWithFormat:"SELF MATCHES ’[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}’"
  
set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list
  
return (length of bRes = 1) as boolean
end chkIPAddressFormat

★Click Here to Open This Script 

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

指定リストから、Regexpで要素を抽出(NSPredicate)

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:指定リストから、Regexpで要素を抽出(NSPredicate)
— Created 2017-10-29 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set stringArray to current application’s NSArray’s arrayWithArray:{"adobe", "22", "microsoft", "99"}
set thePred to current application’s NSPredicate’s predicateWithFormat:"self MATCHES ’\\\\d\\\\d’"
set bList to (stringArray’s filteredArrayUsingPredicate:thePred) as list
–>  {​​​​​"22", ​​​​​"99"​​​}

★Click Here to Open This Script 

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

正規表現で数字を抽出(NSRegularExpressionSearch)

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:正規表現で数字を抽出(NSRegularExpressionSearch)
— Created 2017-12-17 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to "126 ひよこ豆コーヒー豆だだちゃ豆"
set n1Res to filterNumStr(aStr) of me
log result
–>  "126"

set aStr to "ひよこ豆コーヒー豆だだちゃ豆 345"
set n2Res to filterNumStr(aStr) of me
log result
–>  "345

set aStr to "ひよこ豆コーヒー豆999だだちゃ豆"
set n3Res to filterNumStr(aStr) of me
log result
–>  999

on filterNumStr(aStr as string)
  set aLen to length of aStr
  
set regStr to "\\d{1," & (aLen as string) & "}"
  
set aRes to findStrByPattern(aStr, regStr) of me
  
return aRes as {boolean, number}
end filterNumStr

on findStrByPattern(aText as string, regStr as string)
  set anNSString to current application’s NSString’s stringWithString:aText
  
set aRange to anNSString’s rangeOfString:regStr options:(current application’s NSRegularExpressionSearch)
  
if aRange = {location:0, length:0} then return ""
  
set bStr to anNSString’s substringWithRange:aRange
  
return bStr as string
end findStrByPattern

★Click Here to Open This Script 

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

Numbers上で選択中の列のデータからIPアドレスを抽出(NSPredicate)

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:Numbers上で選択中の列のデータからIPアドレスを抽出(NSPredicate)
— Created 2018-01-03 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–http://piyocast.com/as/archives/5080

property NSPredicate : a reference to current application’s NSPredicate
property NSArray : a reference to current application’s NSArray

set aList to getSelectionDataFromNumbers() of me
set anArray to NSArray’s arrayWithArray:aList
set aPred to NSPredicate’s predicateWithFormat:"SELF MATCHES ’[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}’"
set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list
–>  {​​​​​"XX.XX.XX.XXX", ​​​​​"XXX.XX.XXX.XXX", ​​​​​"XXX.XXX.XX.XXX", …..}

on getSelectionDataFromNumbers()
  tell application "Numbers"
    tell front document
      tell active sheet
        tell table 1
          set aList to value of every cell of selection range
        end tell
      end tell
    end tell
  end tell
  
return aList
end getSelectionDataFromNumbers

★Click Here to Open This Script 

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

連番JPEGファイルを読み込んで連結したPDFを作成(新規作成)

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:連番JPEGファイルを読み込んで連結したPDFを作成(新規作成)
— Created 2016-09-20 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "QuartzCore"
use framework "Quartz"
use framework "AppKit"

set aExt to ".jpg"
set aFol to choose folder
set fList to getFilePathList(aFol, aExt) of me
set f2List to my sort1DList:fList ascOrder:true –sort by ascending

set newFile to POSIX path of (choose file name with prompt "新規PDFファイルの名称を選択")
set newFilePath to current application’s NSString’s stringWithString:newFile

–Make Blank PDF
set aPDFdoc to current application’s PDFDocument’s alloc()’s init()

set pageNum to 0

repeat with i in f2List
  set j to contents of i
  
set aURL to (current application’s |NSURL|’s fileURLWithPath:j)
  
set bImg to (current application’s NSImage’s alloc()’s initWithContentsOfURL:aURL)
  (
aPDFdoc’s insertPage:(current application’s PDFPage’s alloc()’s initWithImage:bImg) atIndex:pageNum)
  
set pageNum to pageNum + 1
end repeat

aPDFdoc’s writeToFile:newFilePath

–ASOCで指定フォルダのファイルパス一覧取得(拡張子指定つき)
on getFilePathList(aFol, aExt)
  set aPath to current application’s NSString’s stringWithString:(POSIX path of aFol)
  
set aFM to current application’s NSFileManager’s defaultManager()
  
set nameList to (aFM’s contentsOfDirectoryAtPath:aPath |error|:(missing value)) as list
  
set anArray to current application’s NSMutableArray’s alloc()’s init()
  
  
repeat with i in nameList
    set j to i as text
    
if (j ends with aExt) and (j does not start with ".") then –exclude invisible files
      set newPath to (aPath’s stringByAppendingString:j)
      (
anArray’s addObject:newPath)
    end if
  end repeat
  
  
return anArray as list
end getFilePathList

–1D List(文字)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート
on sort1DList:theList ascOrder:aBool
  set aDdesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:"self" ascending:aBool selector:"localizedCaseInsensitiveCompare:"
  
set theArray to current application’s NSArray’s arrayWithArray:theList
  
return (theArray’s sortedArrayUsingDescriptors:{aDdesc}) as list
end sort1DList:ascOrder:

★Click Here to Open This Script 

Posted in Image PDF | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

連番JPEGファイルを読み込んで連結したPDFを作成(既存のPDFに追加)

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:連番JPEGファイルを読み込んで連結したPDFを作成(既存のPDFに追加)
— Created 2016-09-20 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "QuartzCore"
use framework "Quartz"
use framework "AppKit"

set aExt to ".jpg"

set targAlias to retFrontFinderWindowsTargetIfExits(path to desktop) of me
set aFol to choose folder with prompt "追記するJPEG画像ファイルが入っているフォルダを選択" default location targAlias

set fList to getFilePathList(aFol, aExt) of me
set f2List to my sort1DList:fList ascOrder:true –sort by ascending

set newFile to POSIX path of (choose file of type {"com.adobe.pdf"} with prompt "既存のPDFファイルを選択(このPDF末尾に画像を追加)")
set newFilePath to current application’s NSString’s stringWithString:newFile
set newFileURL to current application’s |NSURL|’s fileURLWithPath:newFile

–Get Exsisting PDF’s URL and Use it
set aPDFdoc to current application’s PDFDocument’s alloc()’s initWithURL:newFileURL
set pageNum to ((aPDFdoc’s pageCount()) as integer)

repeat with i in f2List
  set j to contents of i
  
set aURL to (current application’s |NSURL|’s fileURLWithPath:j)
  
set bImg to (current application’s NSImage’s alloc()’s initWithContentsOfURL:aURL)
  (
aPDFdoc’s insertPage:(current application’s PDFPage’s alloc()’s initWithImage:bImg) atIndex:pageNum)
  
set pageNum to pageNum + 1
end repeat

aPDFdoc’s writeToFile:newFilePath

–ASOCで指定フォルダのファイルパス一覧取得(拡張子指定つき)
on getFilePathList(aFol, aExt)
  set aPath to current application’s NSString’s stringWithString:(POSIX path of aFol)
  
set aFM to current application’s NSFileManager’s defaultManager()
  
set nameList to (aFM’s contentsOfDirectoryAtPath:aPath |error|:(missing value)) as list
  
set anArray to current application’s NSMutableArray’s alloc()’s init()
  
  
repeat with i in nameList
    set j to i as text
    
if (j ends with aExt) and (j does not start with ".") then –exclude invisible files
      set newPath to (aPath’s stringByAppendingString:j)
      (
anArray’s addObject:newPath)
    end if
  end repeat
  
  
return anArray as list
end getFilePathList

–1D List(文字)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート
on sort1DList:theList ascOrder:aBool
  set aDdesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:"self" ascending:aBool selector:"localizedCaseInsensitiveCompare:"
  
set theArray to current application’s NSArray’s arrayWithArray:theList
  
return (theArray’s sortedArrayUsingDescriptors:{aDdesc}) as list
end sort1DList:ascOrder:

on retFrontFinderWindowsTargetIfExits(aDefaultLocation)
  tell application "Finder"
    set wCount to count every window
    
if wCount ≥ 1 then
      tell front window
        set aTarg to target as alias
      end tell
      
return aTarg
    else
      return aDefaultLocation
    end if
  end tell
end retFrontFinderWindowsTargetIfExits

★Click Here to Open This Script 

Posted in Image PDF | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指数表示数値を文字列化

Posted on 2月 24, 2018 by Takaaki Naganoya

指数表示の数値を文字列化するAppleScriptです。

AppleScriptの数値変数で表現可能な範囲は割と狭く、±1.79769E308。実数部は9桁です。

そのため、不意に指数表示になってしまったデータから指数表現を解除して、数値文字列に変換し、適宜bcコマンドなどで数値文字列同士の演算を行うことがままあります。そういう場合に使用します。

AppleScript text item delimitersを活用していることから、あきらかに自分が組んだScriptではありません(なるべく避けるので)。たぶん、Mailing Listに流れていたScriptです。

AppleScript名:指数表示数値を文字列化
set longNumber to "1082204521"
set exponent to longNumber as number –> 1.082204521E+9
set numberString to Stringify(exponent)

on Stringify(x) — for E+ numbers
  set x to x as string
  
set {tids, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, {"E+"}}
  
if (count (text items of x)) = 1 then
    set AppleScript’s text item delimiters to {tids}
    
return x
  else
    set {n, z} to {text item 1 of x, (text item 2 of x) as integer}
    
set AppleScript’s text item delimiters to {tids}
    
set i to character 1 of n
    
set decSepChar to character 2 of n — "." or ","
    
set d to text 3 thru -1 of n
    
set l to count d
    
if l > z then
      return (i & (text 1 thru z of d) & decSepChar & (text (z + 1) thru -1 of d))
    else
      repeat (z – l) times
        set d to d & "0"
      end repeat
      
return (i & d)
    end if
  end if
end Stringify

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

真偽値の反転

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:真偽値の反転
set a to true

set b to not a
–> false

★Click Here to Open This Script 

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

序数を求めるルーチン同士を比較

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:序数を求めるルーチン同士を比較
repeat with i from 1 to 1000
  set a to retOrdinalNumStr(i) of me
  
set b to getOrdinalNumber(i) of me
  
  
log {a, b}
end repeat

–数値を与えると序数の文字列を返す
on retOrdinalNumStr(aNum)
  set aStr to aNum as string
  
  
–下1桁の数字を取得
  
set last1Str to last character of aStr
  
  
–下2桁目の数字を取得
  
if length of aStr > 1 then
    set last2Str to character -2 of aStr
  else
    set last2Str to ""
  end if
  
  
–場合分け
  
set retStr to ""
  
if last1Str = "1" then
    if last2Str = "1" then
      set retStr to "th" –11
    else
      set retStr to "st"
    end if
    
  else if last1Str = "2" then
    if last2Str = "1" then
      set retStr to "th" –12
    else
      set retStr to "nd"
    end if
    
  else if last1Str = "3" then
    if last2Str = "1" then
      set retStr to "th" –13
    else
      set retStr to "rd"
    end if
    
  else
    set retStr to "th"
    
  end if
  
  
return aStr & retStr
  
end retOrdinalNumStr

— 序数表現に変更
on getOrdinalNumber(anyInteger)
  
  
if (anyInteger mod 10) = 1 and (anyInteger mod 100) ≠ 11 then
    set tempList to anyInteger & "st"
  else if (anyInteger mod 10) = 2 and (anyInteger mod 100) ≠ 12 then
    set tempList to anyInteger & "nd"
  else if (anyInteger mod 10) = 3 and (anyInteger mod 100) ≠ 13 then
    set tempList to anyInteger & "rd"
  else
    set tempList to anyInteger & "th"
  end if
  
  
return tempList as text
  
end getOrdinalNumber

★Click Here to Open This Script 

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

奇数かどうかチェック

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:奇数かどうかチェック
set a to 5
set aRes to chkOddNum(a) of me
–> true

set b to 8
set bRes to chkEvenNum(b) of me
–> true

–奇数かどうかチェック
on chkOddNum(aNum)
  set a to aNum mod 2
  
if a = 1 then
    return true
  else
    return false
  end if
end chkOddNum

–偶数かどうかチェック
on chkEvenNum(aNum)
  set a to aNum mod 2
  
if a = 0 then
    return true
  else
    return false
  end if
end chkEvenNum

★Click Here to Open This Script 

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

文字で書いた数値を本物の数値に変換

Posted on 2月 24, 2018 by Takaaki Naganoya

仰々しくタイトルを書いていますが、なんのことはない文字で書いた数字文字列の数値へのcastを行う処理です。

AppleScriptのネイティブのやりかただと、

set a to "12345"
set b to a as number

という程度ですみます(Adobe Illustratorへのtellブロック内で上記のScriptを実行しないでください。L*a*b*色空間のaとbの1文字の予約語を持っているので、動作がおかしくなります)。

本Scriptで紹介しているのは、Cocoaのオブジェクトの数値文字列をCocoaの数値オブジェクト(NSNumber)に変換するものです。

本来はNSNumberのままなのですが、Scripting Bridgeの仕様でNSNumberになったものは自動でAppleScriptの数値まで変換されます。

AppleScript名:文字で書いた数値を本物の数値に変換
— Created 2014-12-28 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set j to "123456789"
set aStr to current application’s NSString’s stringWithString:j
set aNum to aStr’s intValue()

★Click Here to Open This Script 

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

数値の桁数を求める v2

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:数値の桁数を求める v2
— Created 2014-12-04 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use bPlus : script "BridgePlus"

set aList to {}
set aRes to 0

repeat with i from 1 to 9999 by 1
  set d to getDigit(i) of me
  
  
if aRes is not equal to d then
    set the end of aList to {i, d}
    
set aRes to d
  end if
end repeat
aList
–> {{1, 1}, {10, 2}, {100, 3}, {1000, 4}}

–数値の桁数を求める(マイナスの数は想定外)
on getDigit(aNum as integer)
  load framework
  
set b to ((current application’s SMSForder’s log10ValueOf:aNum))
  
set c to b as real
  
set d to round c rounding down
  
return (d + 1)
end getDigit

★Click Here to Open This Script 

BridgePlusがmacOS 10.15以降でうまく動かない環境もあるので、数値演算ライブラリcalcLibASを呼び出すものも掲載しておきます。

AppleScript名:数値の桁数を求める v3.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/06/30
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use calSub : script "calcLibAS" –http://piyocast.com/as/asinyaye

set aRes1 to getNumDigit(-9999) of me
–> 3

set aRes2 to getNumDigit(100) of me
–> 2

on getNumDigit(aNum)
  set a2Val to abs aNum
  
return round (log10 a2Val) rounding down
end getNumDigit

★Click Here to Open This Script 

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

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • Numbersで選択範囲のセルの前後の空白を削除
  • macOS 26, Tahoe
  • macOS 15でも変化したText to Speech環境
  • KagiのWebブラウザ、Orion
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • Script Debuggerの開発と販売が2025年に終了
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • macOS 14で変更になったOSバージョン取得APIの返り値
  • macOS 15 リモートApple Eventsにバグ?
  • NSObjectのクラス名を取得 v2.1
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • 2024年に書いた価値あるAppleScript
  • 有害ではなくなっていたSpaces
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行
  • Pixelmator Proがv3.6.8でHDR画像をサポート
  • (確認中)AppleScript Dropletのバグっぽい動作が解消?
  • AVSpeechSynthesizerで読み上げテスト
  • AppleScript Dropletのバグっぽい動作が「復活」(macOS 15.5β)
  • 指定フォルダ以下の画像のMD5チェックサムを求めて、重複しているものをピックアップ
  • macOS 26, 15.5でShortcuts.app「AppleScriptを実行」アクションのバグが修正される

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (203) 14.0savvy (158) 15.0savvy (150) CotEditor (66) Finder (52) 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 (56) Pixelmator Pro (20) 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
  • Scripting Additions
  • 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年9月
  • 2025年8月
  • 2025年7月
  • 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