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

月: 2018年2月

与えられたデータのうち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

数値にゼロパディングしたテキストを返す

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:数値にゼロパディングしたテキストを返す
set aNum to 1
set aStr to retZeroPaddingText(aNum, 4) of me

–数値にゼロパディングしたテキストを返す
on retZeroPaddingText(aNum, aLen)
  set tText to ("0000000000" & aNum as text)
  
set tCount to length of tText
  
set resText to text (tCount – aLen + 1) thru tCount of tText
  
return resText
end retZeroPaddingText

★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名:数値を指定桁でゼロパディング
— Created 2015-08-05 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aTEXT to numToZeroPaddingStr(99, 4, "0")
–>  "0099"

set aTEXT to numToZeroPaddingStr(99999999, 20, "0")
–> "00000000000099999999"

–整数の値に指定桁数ゼロパディングして文字列で返す
on numToZeroPaddingStr(aNum as integer, aDigit as integer, paddingChar as text)
  
  
set aNumForm to current application’s NSNumberFormatter’s alloc()’s init()
  
aNumForm’s setPaddingPosition:(current application’s NSNumberFormatterPadBeforePrefix)
  
aNumForm’s setPaddingCharacter:paddingChar
  
aNumForm’s setMinimumIntegerDigits:aDigit
  
  
set bNum to current application’s NSNumber’s numberWithInt:aNum
  
set aStr to aNumForm’s stringFromNumber:bNum
  
  
return aStr as text
  
end numToZeroPaddingStr

★Click Here to Open This Script 

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

ローマ数字エンコーダー、デコーダー v2

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:ローマ数字エンコーダー、デコーダー v2
— Created 2015-11-16 by Takaaki Naganoya
— Modified 2017-05-18 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–http://stackoverflow.com/questions/14762282/how-to-convert-a-roman-numeral-to-an-nsnumer

repeat with i from 1 to 3999
  set aRes to numberToRomanNumerals(i) of me
  
set bRes to romanNumeralsToNumber(aRes) of me
  
log {i, aRes, bRes}
end repeat

–Roman Number String Encoder
on numberToRomanNumerals(aNum)
  if (aNum < 0 or aNum > 3999) then return "" –条件が間違っていたので直した(2017/5/18)
  
  
set r_ones to {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
  
set r_tens to {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
  
set r_hunds to {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
  
set r_thouds to {"M", "MM", "MMM", "MMMM", "MMMMM", "MMMMMM", "MMMMMMM", "MMMMMMMM", "MMMMMMMMM"}
  
  
set ones to aNum mod 10
  
set tens to (aNum – ones) mod 100
  
set hundreds to (aNum – tens – ones) mod 1000
  
set thou to (aNum – hundreds – tens – ones) mod 10000
  
  
set tens to tens / 10
  
set hundreds to hundreds / 100
  
set thou to thou / 1000
  
  
set rNum to current application’s NSString’s stringWithString:""
  
  
if thou > 0 then set rNum to rNum’s stringByAppendingString:(item thou of r_thouds)
  
if hundreds > 0 then set rNum to rNum’s stringByAppendingString:(item hundreds of r_hunds)
  
if tens > 0 then set rNum to rNum’s stringByAppendingString:(item tens of r_tens)
  
if ones > 0 then set rNum to rNum’s stringByAppendingString:(item ones of r_ones)
  
  
return rNum as text
end numberToRomanNumerals

–Roman Number String Decoder
on romanNumeralsToNumber(str)
  — Piero Garzotto (http://scriptbuilders.net/files/romannumerals1.0.html)
  
local str, r, i
  
set r to 0
  
repeat with i from 1 to count str
    set r to r + (item (offset of (item i of str) in "mdclxvi") of ¬
      {1000, 500, 100, 50, 10, 5, 1}) * (((((offset of ¬
      (
item (i + 1) of (str & "@")) in "mdclxvi@") ≥ (offset of ¬
      (
item i of str) in "mdclxvi")) as integer) * 2) – 1)
  end repeat
  
return r
end romanNumeralsToNumber

★Click Here to Open This Script 

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

ABSを求める

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:ABSを求める
set x to -44
set x to absNum(x)
–> 44

on absNum(q)
  if q is less than 0 then set q to –q
  
return q
end absNum

★Click Here to Open This Script 

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

同スコアを考慮した順位決定(汎用ルーチン)

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:同スコアを考慮した順位決定(汎用ルーチン)
use AppleScript version "2.4"
use scripting additions

set rList to {{"近 装甲強化型ジム COST: 200", 60}, {"遠 ジム・キャノン COST: 160", 39}, {"近 ジム・コマンド COST: 200", 32}, {"近 ジム(WD隊) COST: 160", 26}, {"近 陸戦型ガンダム COST: 220", 23}, {"近 ジム改 COST: 240", 22}, {"遠 ガンタンク COST: 200", 21}, {"格 ジム(指揮官機) COST: 160", 20}, {"近 ジム COST: 120", 19}, {"格 陸戦型ジム COST: 120", 10}, {"近 ジム・トレーナー COST: 120", 9}, {"射 ジム・スナイパーII(WD隊) COST: 220", 8}, {"射 陸戦型ガンダム(ジム頭) COST: 200", 7}, {"格 ガンダム COST: 280", 6}, {"近 ザクII(F2) COST: 160", 6}, {"格 ジム・ストライカー COST: 180", 4}, {"近 ジム・寒冷地仕様 COST: 200", 4}, {"狙 ジム・スナイパーカスタム COST: 200", 3}, {"近 アクア・ジム COST: 160", 2}, {"遠 量産型ガンタンク COST: 160", 2}, {"格 ガンキャノン重装型 COST: 160", 1}, {"近 ジム・コマンドライトアーマー COST: 160", 1}, {"射 ガンキャノン COST: 200", 1}, {"格 ボールK型 COST: 120", 0}, {"射 デザート・ジム COST: 160", 0}}

set rRes to retRankingList(rList) of me
–> {{rank:1, aCon:"近 装甲強化型ジム COST: 200", aTimes:60}, {rank:2, aCon:"遠 ジム・キャノン COST: 160", aTimes:39}, {rank:3, aCon:"近 ジム・コマンド COST: 200", aTimes:32}, {rank:4, aCon:"近 ジム(WD隊) COST: 160", aTimes:26}, {rank:5, aCon:"近 陸戦型ガンダム COST: 220", aTimes:23}, {rank:6, aCon:"近 ジム改 COST: 240", aTimes:22}, {rank:7, aCon:"遠 ガンタンク COST: 200", aTimes:21}, {rank:8, aCon:"格 ジム(指揮官機) COST: 160", aTimes:20}, {rank:9, aCon:"近 ジム COST: 120", aTimes:19}, {rank:10, aCon:"格 陸戦型ジム COST: 120", aTimes:10}, {rank:11, aCon:"近 ジム・トレーナー COST: 120", aTimes:9}, {rank:12, aCon:"射 ジム・スナイパーII(WD隊) COST: 220", aTimes:8}, {rank:13, aCon:"射 陸戦型ガンダム(ジム頭) COST: 200", aTimes:7}, {rank:14, aCon:"格 ガンダム COST: 280", aTimes:6}, {rank:14, aCon:"近 ザクII(F2) COST: 160", aTimes:6}, {rank:16, aCon:"格 ジム・ストライカー COST: 180", aTimes:4}, {rank:16, aCon:"近 ジム・寒冷地仕様 COST: 200", aTimes:4}, {rank:18, aCon:"狙 ジム・スナイパーカスタム COST: 200", aTimes:3}, {rank:19, aCon:"近 アクア・ジム COST: 160", aTimes:2}, {rank:19, aCon:"遠 量産型ガンタンク COST: 160", aTimes:2}, {rank:21, aCon:"格 ガンキャノン重装型 COST: 160", aTimes:1}, {rank:21, aCon:"近 ジム・コマンドライトアーマー COST: 160", aTimes:1}, {rank:21, aCon:"射 ガンキャノン COST: 200", aTimes:1}, {rank:24, aCon:"格 ボールK型 COST: 120", aTimes:0}, {rank:24, aCon:"射 デザート・ジム COST: 160", aTimes:0}}

on retRankingList(rList)
  set aText to {} –出力用リスト
  
set aCount to 0
  
  
set prevPoint to -1
  
set buffCount to 0
  
  
  
repeat with i in rList
    
    
copy i to {j, bCount}
    
    
if prevPoint = bCount then
      –同じポイントが続いた場合
      
if buffCount is not equal to 0 then
        –同じポイントが複数回(2回以上)続いている場合
        
set buffCount to buffCount + 1
        
log {"case A:", buffCount, aCount, bCount, prevPoint}
      else
        –同じポイントが続いている場合(2回目)
        
set buffCount to 1
        
log {"case B:", buffCount, aCount, bCount, prevPoint}
      end if
    else
      –同じポイントが続いていない場合
      
if buffCount is not equal to 0 then
        –直前まで同じポイントが続いていた場合
        
set aCount to aCount + buffCount + 1 –バッファしておいたカウントを出力する
        
set buffCount to 0
        
log {"case C:", buffCount, aCount, bCount, prevPoint}
      else
        –通常パターン(直前まで同じポイントが続いていたりしない)
        
set buffCount to 0
        
set aCount to aCount + 1 –ランキング順位を+1
        
log {"case D:", buffCount, aCount, bCount, prevPoint}
      end if
    end if
    
    
set the end of aText to {rank:aCount, aCon:j as string, aTimes:bCount}
    
copy bCount to prevPoint
    
  end repeat
  
  
return aText
  
end retRankingList

–リストを任意のデリミタ付きでテキストに
on retArrowText(aList, aDelim)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end retArrowText

★Click Here to Open This Script 

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

平方根(SQR)を求める

Posted on 2月 23, 2018 by Takaaki Naganoya

平方根(SQR)を求める処理です。このほか、JavaScript Coreで関数計算を行わせるライブラリ「calcLibAS」を用いる方法があります。

AppleScript名:平方根(SQR)を求める

set a to getSQR(4) of me
–> 2.0

–平方根を求める
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

数値を日本語文字列に

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:数値を日本語文字列に
— Created 2016-11-13 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set theValue to (current application’s NSNumberFormatter’s localizedStringFromNumber:287654321 numberStyle:(current application’s NSNumberFormatterSpellOutStyle)) as string
–>  "二億八千七百六十五万四千三百二十一"

★Click Here to Open This Script 

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

日本語的数値表現エンコーダーv2

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:日本語的数値表現エンコーダーv2
–課題:オーバーフローチェックを行っていない
set a to "102320120000108220010"
set jRes to encodeJapaneseNumText(a) of japaneseNumberEncodingKit
–> "1垓232京120兆1億822万10"

script japaneseNumberEncodingKit
  –数字文字列を日本語数値表現文字列に変換
  
on encodeJapaneseNumText(aNum)
    
    
set aText to Stringify(aNum) of me
    
set aText to aText as Unicode text
    
set dotText to "." as Unicode text
    
set upperDigit to ""
    
set lowerDigit to ""
    
    
–小数点の処理
    
if dotText is in aText then
      set b to offset of dotText in aText
      
set upperDigit to characters 1 thru (b – 1) of aText
      
set upperDigit to upperDigit as Unicode text
      
set lowerDigit to characters b thru -1 of aText
      
set lowerDigit to lowerDigit as Unicode text
    else
      set upperDigit to aText
    end if
    
    
    
set scaleList3 to {"", "万", "億", "兆", "京", "垓", "丈", "壌", "溝", "砂", "正", "載", "極", "恒河沙", "阿僧梢", "那由他", "不可思議", "無量大数"}
    
set splitDigit to 4
    
set nList to splitByDigit(upperDigit, splitDigit) of me
    
set nList to reverse of nList
    
    
set resText to ""
    
set digCount to 1
    
repeat with i in nList
      set b to (contents of i) as number
      
if b is not equal to 0 then
        set resText to (b as text) & item digCount of scaleList3 & resText
      end if
      
set digCount to digCount + 1
    end repeat
    
    
    
    
return resText & lowerDigit
    
  end encodeJapaneseNumText
  
  
–指定桁数で区切る
  
on splitByDigit(a, splitDigit)
    set aList to characters of a
    
set aList to reverse of aList
    
log aList
    
set resList to {}
    
set tempT to ""
    
set tempC to 1
    
repeat with i in aList
      set tempT to contents of i & tempT
      
if tempC mod splitDigit = 0 then
        set resList to {tempT} & resList
        
set tempT to ""
      end if
      
set tempC to tempC + 1
    end repeat
    
    
if tempT is not equal to "" then
      set resList to {tempT} & resList
    end if
    
    
resList
    
  end splitByDigit
  
  
  
  
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
end script

★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

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • Xcode 14.2でAppleScript App Templateを復活させる
  • UI Browserがgithub上でソース公開され、オープンソースに
  • macOS 13 TTS Voice環境に変更
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた
  • 従来と異なるmacOS 13の性格?
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • AS関連データの取り扱いを容易にする(はずの)privateDataTypeLib
  • macOS 13でNSNotFoundバグふたたび
  • macOS 12.5.1、11.6.8でFinderのselectionでスクリーンショット画像をopenできない問題
  • ChatGPTでchatに対する応答文を取得
  • 新発売:iWork Scripting Book with AppleScript
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (277) 12.0savvy (186) 13.0savvy (59) CotEditor (60) Finder (47) iTunes (19) Keynote (99) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (57) Pages (38) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • 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
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • 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)
  • 未分類

アーカイブ

  • 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