縦横比計算(修正 1対Xを追加)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | (* |
005 | ダイアログ部ベーススクリプト |
006 | https://www.macscripter.net/t/edit-db123s-dialog-for-use-with-asobjc/73636/2 |
007 | *) |
008 | #com.cocolog-nifty.quicktimer.icefloe |
009 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
010 | use AppleScript version "2.8" |
011 | use framework "Foundation" |
012 | use framework "UniformTypeIdentifiers" |
013 | use framework "AppKit" |
014 | #use framework "Carbon" |
015 | use scripting additions |
016 | property refMe : a reference to current application |
017 | |
018 | doDialoge() |
019 | |
020 | ##################### |
021 | ### |
022 | property appDialogWindow : missing value |
023 | property strOneTextField : missing value |
024 | property strTwoTextField : missing value |
025 | property appCancelButton : missing value |
026 | property appOkButton : missing value |
027 | property strOne : missing value |
028 | property strTwo : missing value |
029 | property appOkClicked : false |
030 | property refNSNotFound : a reference to 9.22337203685477E+18 + 5807 |
031 | |
032 | |
033 | to doDialoge() |
034 | |
035 | ############################## |
036 | #####ダイアログ |
037 | ############################## |
038 | tell current application |
039 | set strName to name as text |
040 | end tell |
041 | ####スクリプトメニューから実行したら |
042 | if strName is "osascript" then |
043 | tell application "Finder" |
044 | activate |
045 | end tell |
046 | else |
047 | tell current application |
048 | activate |
049 | end tell |
050 | end if |
051 | |
052 | set dialogResult to my doShowDialog() |
053 | if dialogResult is missing value then |
054 | return "【エラー】キャンセルしました" |
055 | end if |
056 | set strReturnedTextX to strOne of dialogResult |
057 | set strReturnedTextY to strTwo of dialogResult |
058 | |
059 | ############################## |
060 | #####戻り値整形 |
061 | ############################## |
062 | set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedTextX)) |
063 | ###タブと改行を除去しておく |
064 | set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
065 | ocidTextM's appendString:(ocidResponseText) |
066 | ##改行除去 |
067 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("") |
068 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("") |
069 | ##タブ除去 |
070 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("") |
071 | ####戻り値を半角にする |
072 | set ocidNSStringTransform to (refMe's NSStringTransformFullwidthToHalfwidth) |
073 | set ocidTextM to (ocidTextM's stringByApplyingTransform:ocidNSStringTransform |reverse|:false) |
074 | ##カンマ置換 |
075 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:(",") withString:(".") |
076 | ###数字以外の値を取る |
077 | #set ocidDecSet to refMe's NSCharacterSet's decimalDigitCharacterSet |
078 | #set ocidCharSet to ocidDecSet's invertedSet() |
079 | #set ocidCharArray to ocidResponseHalfwidth's componentsSeparatedByCharactersInSet:ocidCharSet |
080 | #set ocidInteger to ocidCharArray's componentsJoinedByString:"" |
081 | ###テキストにしてから |
082 | set strTextM to ocidTextM as text |
083 | ###数値に |
084 | set strReturnedTextX to strTextM as number |
085 | |
086 | ### |
087 | set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedTextY)) |
088 | ###タブと改行を除去しておく |
089 | set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
090 | ocidTextM's appendString:(ocidResponseText) |
091 | ##改行除去 |
092 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("") |
093 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("") |
094 | ##タブ除去 |
095 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("") |
096 | ####戻り値を半角にする |
097 | set ocidNSStringTransform to (refMe's NSStringTransformFullwidthToHalfwidth) |
098 | set ocidTextM to (ocidTextM's stringByApplyingTransform:ocidNSStringTransform |reverse|:false) |
099 | ##カンマ置換 |
100 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:(",") withString:(".") |
101 | ###数字以外の値を取る |
102 | #set ocidDecSet to refMe's NSCharacterSet's decimalDigitCharacterSet |
103 | #set ocidCharSet to ocidDecSet's invertedSet() |
104 | #set ocidCharArray to ocidResponseHalfwidth's componentsSeparatedByCharactersInSet:ocidCharSet |
105 | #set ocidInteger to ocidCharArray's componentsJoinedByString:"" |
106 | ###テキストにしてから |
107 | set strTextM to ocidTextM as text |
108 | ###数値に |
109 | set strReturnedTextY to strTextM as number |
110 | |
111 | ############################## |
112 | #####計算部 |
113 | ############################## |
114 | |
115 | if (strReturnedTextX as number) ≥ (strReturnedTextY as number) then |
116 | set numRaw to (strReturnedTextX / strReturnedTextY) as number |
117 | else if (strReturnedTextY as number) > (strReturnedTextX as number) then |
118 | set numRaw to (strReturnedTextY / strReturnedTextX) as number |
119 | end if |
120 | ### |
121 | set ocidDecimalNumber to refMe's NSDecimalNumber's decimalNumberWithString:(numRaw as text) |
122 | set appNumberFormatter to refMe's NSNumberFormatter's alloc()'s init() |
123 | appNumberFormatter's setPositiveFormat:("#,##0.00") |
124 | set ocidRetio to appNumberFormatter's stringFromNumber:(ocidDecimalNumber) |
125 | ###そのまま |
126 | set strMes to ("計算結果です: 1 対 " & (ocidRetio as text) & " \r") as text |
127 | set strMes to ("Number値: " & numRaw & " \r") as text |
128 | ###整数切り捨て |
129 | set intDown to ((round of ((numRaw) * 1000) rounding down) / 1000) * 100 as number |
130 | log intDown |
131 | set strMes to (strMes & "パーセント:" & intDown & "% \r") as text |
132 | |
133 | ###切り捨て 小数点2 |
134 | set num2Dec to ((round of ((numRaw) * 100) rounding down) / 100) as number |
135 | log num2Dec |
136 | set strMes to (strMes & "小数点2位:" & num2Dec & "\r") as text |
137 | ###切り捨て 小数点3 |
138 | set num3Dec to ((round of ((numRaw) * 1000) rounding down) / 1000) as number |
139 | log num3Dec |
140 | set strMes to (strMes & "小数点3位:" & num3Dec & "\r") as text |
141 | ###切り捨て 小数点4 |
142 | set num4Dec to ((round of ((numRaw) * 10000) rounding down) / 10000) as number |
143 | log num4Dec |
144 | set strMes to (strMes & "小数点4位:" & num4Dec & "\r") as text |
145 | #### |
146 | set strMes to (strMes & "小数点以下計算は切り捨て\r") as text |
147 | ############################## |
148 | #####ダイアログ |
149 | ############################## |
150 | tell current application |
151 | set strName to name as text |
152 | end tell |
153 | ####スクリプトメニューから実行したら |
154 | if strName is "osascript" then |
155 | tell application "Finder" |
156 | activate |
157 | end tell |
158 | else |
159 | tell current application |
160 | activate |
161 | end tell |
162 | end if |
163 | set aliasIconPath to POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns" as alias |
164 | |
165 | try |
166 | set recordResult to (display dialog strMes with title strMes default answer numRaw buttons {"クリップボードにコピー", "もう一度", "終了"} default button "もう一度" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record |
167 | on error |
168 | log "エラーしました" |
169 | return |
170 | end try |
171 | log recordResult |
172 | if button returned of recordResult is "もう一度" then |
173 | doDialoge() |
174 | else if (gave up of recordResult) is true then |
175 | return "時間切れです" |
176 | |
177 | end if |
178 | ############################## |
179 | #####値のコピー |
180 | ############################## |
181 | if button returned of recordResult is "クリップボードにコピー" then |
182 | try |
183 | set strText to text returned of recordResult as text |
184 | ####ペーストボード宣言 |
185 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
186 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
187 | appPasteboard's clearContents() |
188 | appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
189 | on error |
190 | tell application "Finder" |
191 | set the clipboard to strText as text |
192 | end tell |
193 | end try |
194 | end if |
195 | end doDialoge |
196 | |
197 | |
198 | |
199 | #################################### |
200 | ###### ダイアログ |
201 | #################################### |
202 | |
203 | on doShowDialog() |
204 | if refMe's AEInteractWithUser(-1, missing value, missing value) ≠ 0 then |
205 | return missing value |
206 | end if |
207 | if refMe's NSThread's isMainThread() then |
208 | my doPerformDialog:(missing value) |
209 | else |
210 | its performSelectorOnMainThread:("doPerformDialog:") withObject:(missing value) waitUntilDone:true |
211 | end if |
212 | if my appOkClicked then |
213 | return {strOne:my strOne as text, strTwo:my strTwo as text} |
214 | end if |
215 | return missing value |
216 | end doShowDialog |
217 | |
218 | on doPerformDialog:(args) |
219 | set strOneLabel to refMe's NSTextField's labelWithString:("X size:") |
220 | strOneLabel's setFrame:(refMe's NSMakeRect(20, 85, 70, 20)) |
221 | |
222 | set my strOneTextField to refMe's NSTextField's textFieldWithString:"" |
223 | strOneTextField's setFrame:(refMe's NSMakeRect(87, 85, 245, 20)) |
224 | strOneTextField's setEditable:true |
225 | strOneTextField's setBordered:true |
226 | strOneTextField's setPlaceholderString:("X数値のみ") |
227 | strOneTextField's setDelegate:(me) |
228 | |
229 | set strTwoLabel to refMe's NSTextField's labelWithString:("Y size:") |
230 | strTwoLabel's setFrame:(refMe's NSMakeRect(20, 55, 70, 20)) |
231 | |
232 | set my strTwoTextField to refMe's NSTextField's textFieldWithString:("") |
233 | strTwoTextField's setFrame:(refMe's NSMakeRect(87, 55, 245, 20)) |
234 | strTwoTextField's setEditable:true |
235 | strTwoTextField's setBordered:true |
236 | strTwoTextField's setPlaceholderString:("Y数値のみ") |
237 | |
238 | set my appCancelButton to refMe's NSButton's buttonWithTitle:"Cancel" target:me action:"doButtonAction:" |
239 | appCancelButton's setFrameSize:{94, 32} |
240 | appCancelButton's setFrameOrigin:{150, 10} |
241 | appCancelButton's setKeyEquivalent:(character id 27) |
242 | |
243 | set my appOkButton to refMe's NSButton's buttonWithTitle:"OK" target:me action:"doButtonAction:" |
244 | appOkButton's setFrameSize:{94, 32} |
245 | appOkButton's setFrameOrigin:{245, 10} |
246 | appOkButton's setKeyEquivalent:return |
247 | appOkButton's setEnabled:false |
248 | |
249 | set ocidWindowSize to refMe's NSMakeRect(0, 0, 355, 125) |
250 | set ocidWinStyle to (refMe's NSWindowStyleMaskTitled as integer) + (refMe's NSWindowStyleMaskClosable as integer) |
251 | set my appDialogWindow to refMe's NSWindow's alloc()'s initWithContentRect:(ocidWindowSize) styleMask:(ocidWinStyle) backing:(refMe's NSBackingStoreBuffered) defer:true |
252 | |
253 | appDialogWindow's contentView()'s addSubview:(strOneLabel) |
254 | appDialogWindow's contentView()'s addSubview:(strOneTextField) |
255 | appDialogWindow's contentView()'s addSubview:(strTwoLabel) |
256 | appDialogWindow's contentView()'s addSubview:(strTwoTextField) |
257 | appDialogWindow's contentView()'s addSubview:(appCancelButton) |
258 | appDialogWindow's contentView()'s addSubview:(appOkButton) |
259 | |
260 | appDialogWindow's setTitle:"比率換算" |
261 | appDialogWindow's setLevel:(refMe's NSModalPanelWindowLevel) |
262 | appDialogWindow's setDelegate:(me) |
263 | appDialogWindow's orderFront:(me) |
264 | appDialogWindow's |center|() |
265 | |
266 | refMe's NSApp's activateIgnoringOtherApps:true |
267 | refMe's NSApp's runModalForWindow:(appDialogWindow) |
268 | end doPerformDialog: |
269 | |
270 | on doButtonAction:(sender) |
271 | if sender is my appOkButton then |
272 | set my strOne to strOneTextField's stringValue() |
273 | set my strTwo to strTwoTextField's stringValue() |
274 | set my appOkClicked to true |
275 | end if |
276 | my appDialogWindow's |close|() |
277 | end doButtonAction: |
278 | |
279 | on controlTextDidChange:(objNotification) |
280 | set sender to objNotification's object() |
281 | if sender is my strOneTextField then |
282 | if sender's stringValue() as text ≠ "" then |
283 | my (appOkButton's setEnabled:true) |
284 | else |
285 | my (appOkButton's setEnabled:false) |
286 | end if |
287 | end if |
288 | end controlTextDidChange: |
289 | |
290 | on windowWillClose:(objNotification) |
291 | refMe's NSApp's stopModal() |
292 | end windowWillClose: |
AppleScriptで生成しました |
| 固定リンク