NSColor

カラースペースで画像にタグをつける


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#  画像データに対して カラーデータとプロファイルの有無をチェック
005# tagを付与して カラーインデクスをつけます
006# ドロップレットになります
007# 書き出しからアプリケーションで保存してください
008# com.cocolog-nifty.quicktimer.icefloe
009----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
010use AppleScript version "2.6"
011use framework "Foundation"
012use framework "AppKit"
013use scripting additions
014
015property refMe : a reference to current application
016#ここで指定するUTIは親要素が望ましい
017property strUTI : "public.image" as text
018
019
020#################################
021#【1】クリックでオープンした場合
022on run
023  #ダイアログ を前面に
024  set strName to (name of current application) as text
025  if strName is "osascript" then
026    tell application "Finder" to activate
027  else
028    tell current application to activate
029  end if
030  #デフォルトパス
031  set appFileManager to refMe's NSFileManager's defaultManager()
032  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
033  set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
034  set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
035  set strMes to ("画像ファイルを選んでください") as text
036  set strPrompt to ("画像ファイルを選んでください") as text
037  try
038    #ダイアログ
039    set listChooseAliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with multiple selections allowed without invisibles and showing package contents) as list
040  on error
041    log "エラーしました"
042    return "エラーしました"
043  end try
044  #ファイルリストを次の処理に渡す
045  set boolDone to doPathSender(listChooseAliasFilePath)
046  
047  #戻り値チェック
048  if boolDone is false then
049    display alert "エラーが発生しました" message "エラーが発生しました"
050    return "エラー終了run"
051  else
052    return "処理終了run"
053  end if
054end run
055
056#################################
057#【2】ドロップした場合
058on open listDropAliasFilePath
059  #########【2−1】UTIチェック
060  #親要素(この属性なら処理する)のUTType
061  set ocidChkUTType to refMe's UTType's typeWithIdentifier:(strUTI)
062  #サブルーチンに渡すリスト
063  set listAliasFilePath to {} as list
064  #ドロップされたアイテムの数だけ繰り返す
065  repeat with itemDropAliasFilePath in listDropAliasFilePath
066    #エイリアス
067    set aliasItemFilePath to itemDropAliasFilePath as alias
068    set strName to (name of current application) as text
069    if strName is "osascript" then
070      tell application "Finder"
071        #Finder情報を取得して
072        set recordInfoFor to info for aliasItemFilePath
073      end tell
074    else
075      tell current application
076        set recordInfoFor to info for aliasItemFilePath
077      end tell
078    end if
079    #UTIを取得
080    set strItemUIT to (type identifier of recordInfoFor) as text
081    set ocidUTType to (refMe's UTType's typeWithIdentifier:(strItemUIT))
082    set ocidParentUTIArray to (ocidUTType's supertypes())'s allObjects()
083    #含まれているか?チェックする
084    set boolContain to (ocidParentUTIArray's containsObject:(ocidChkUTType))
085    if boolContain is true then
086      log "含まれているので処理対象"
087      copy aliasItemFilePath to end of listAliasFilePath
088    else if boolContain is false then
089      log "含まれてない"
090    end if
091  end repeat
092  
093  #########【2−1】KINDチェック
094  set listSendAliasFilePath to {} as list
095  repeat with itemAliasFilePath in listAliasFilePath
096    set aliasFilePath to itemAliasFilePath as alias
097    #処理する判定
098    set boolChkAliasPath to true as boolean
099    try
100      tell application "Finder"
101        set strKind to (kind of aliasFilePath) as text
102      end tell
103      
104      if strKind is "アプリケーション" then
105        log "アプリケーションは処理しない"
106        set boolChkAliasPath to false as boolean
107      else if strKind is "ボリューム" then
108        log "ボリュームは処理しない"
109        set boolChkAliasPath to false as boolean
110      else if strKind is "エイリアス" then
111        log "エイリアスは処理しない"
112        set boolChkAliasPath to false as boolean
113      else if strKind is "フォルダ" then
114        log "フォルダは処理しない"
115        set boolChkAliasPath to false as boolean
116      end if
117    on error
118      log "シンボリックリンク等kindを取得できないファイルは処理しない"
119      set boolChkAliasPath to false as boolean
120    end try
121    if boolChkAliasPath is true then
122      copy aliasFilePath to end of listSendAliasFilePath
123    end if
124  end repeat
125  
126  #########【2−3】次工程に渡す
127  set boolDone to doPathSender(listSendAliasFilePath)
128  if boolDone is not true then
129    return false
130  else
131    return "処理終了open"
132  end if
133end open
134
135#################################
136#【3】1と2からファイルパスのエイリアスを受け取り
137#処理に順番に渡す
138to doPathSender(argListAliasFilePath)
139  set appFileManager to refMe's NSFileManager's defaultManager()
140  #1回でいい処理はここに記述する
141  
142  ####1ファイルパス毎本処理に渡す
143  repeat with itemAliasFilePath in argListAliasFilePath
144    #■本処理に渡す
145    set boolDone to doAction(itemAliasFilePath)
146    #戻り値がエラーだったら?
147    if boolDone is false then
148      tell application "Finder"
149        set strFileName to (name of itemAliasFilePath) as text
150      end tell
151      set strMes to (strFileName & "でエラーになりました") as text
152      display alert "エラーが発生しました" message strMes
153      return strMes
154      #エラーになったところで止める
155    end if
156  end repeat
157  return true
158end doPathSender
159
160#################################
161#【4】実際の処理は全てここ
162to doAction(argAliasFilePath)
163  set appFileManager to refMe's NSFileManager's defaultManager()
164  set aliasFilePath to argAliasFilePath as alias
165  set strFilePath to (POSIX path of aliasFilePath) as text
166  set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
167  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
168  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
169  ##タグを取り出しておく
170  set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLTagNamesKey) |error| :(reference))
171  if (item 1 of listResponse) is true then
172    if (item 2 of listResponse) ≠ (missing value) then
173      set ocidTagNamesKeyArray to (item 2 of listResponse)
174    else if (item 2 of listResponse) = (missing value) then
175      set ocidTagNamesKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
176    end if
177  else
178    return false
179  end if
180  ##現在のラベルNOを取り出しておく
181  set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLLabelNumberKey) |error| :(reference))
182  if (item 1 of listResponse) is true then
183    set ocidLabelNo to (item 2 of listResponse)
184  else
185    return false
186  end if
187  
188  ##NSDATAに読み込む
189  set ocidOption to (refMe's NSDataReadingMappedIfSafe)
190  set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference)
191  if (item 2 of listResponse) = (missing value) then
192    set ocidReadData to (item 1 of listResponse)
193  else
194    return false
195  end if
196  ##NSIMAGE
197  set ocidReadImage to refMe's NSImage's alloc()'s initWithData:(ocidReadData)
198  #ImageRep
199  set ocidImageRepArray to ocidReadImage's representations()
200  set ocidImageRep to ocidImageRepArray's firstObject()
201  
202  ####################
203  #プロファイルの有無
204  set ocidColorProfileData to ocidImageRep's valueForProperty:(refMe's NSImageColorSyncProfileData)
205  set ocidColorSpace to refMe's NSColorSpace's alloc()'s initWithICCProfileData:(ocidColorProfileData)
206  #
207  if ocidColorProfileData ≠ (missing value) then
208    set ocidProfileName to ocidColorSpace's localizedName()
209    set strProfileName to ocidProfileName as text
210  else
211    #カラープロファイルが無い場合
212    set ocidTagName to (refMe's NSString's stringWithString:("ColorProfile無"))
213    (ocidTagNamesKeyArray's addObject:(ocidTagName))
214    #重複削除
215    set ocidArraySet to refMe's NSSet's setWithArray:(ocidTagNamesKeyArray)
216    set ocidSetTagNamesKeyArray to ocidArraySet's allObjects()
217    set listDone to (ocidFilePathURL's setResourceValue:(ocidSetTagNamesKeyArray) forKey:(refMe's NSURLTagNamesKey) |error| :(reference))
218    if listDone is false then
219      return false
220    end if
221    #ラベルを赤にする
222    set ocidLabelNo to refMe's NSNumber's numberWithInteger:(6)
223    set listDone to (ocidFilePathURL's setResourceValue:(ocidLabelNo) forKey:(refMe's NSURLLabelNumberKey) |error| :(reference))
224    if listDone is false then
225      return false
226    end if
227  end if
228  ####################
229  #カラースペース
230  set ocidColorSpaceName to ocidImageRep's colorSpaceName()
231  set strColorSpaceName to ocidColorSpaceName as text
232  #
233  if strColorSpaceName contains "DeviceCMYK" then
234    set strSetColorName to ("DeviceCMYK") as text
235    set numIndexNo to 5 as integer
236  else if strColorSpaceName contains "CalibratedCMYK" then
237    set strSetColorName to ("CalibratedCMYK") as text
238    set numIndexNo to 5 as integer
239  else if strColorSpaceName contains "DeviceRGB" then
240    set strSetColorName to ("DeviceRGB") as text
241    set numIndexNo to 2 as integer
242  else if strColorSpaceName contains "CalibratedRGB" then
243    set strSetColorName to ("CalibratedRGB") as text
244    set numIndexNo to 2 as integer
245  else if strColorSpaceName contains "DeviceWhite" then
246    set strSetColorName to ("DeviceWhite") as text
247    set numIndexNo to 1 as integer
248  else if strColorSpaceName contains "CalibratedWhite" then
249    set strSetColorName to ("CalibratedWhite") as text
250    set numIndexNo to 1 as integer
251  else if strColorSpaceName contains "NamedColor" then
252    set strSetColorName to ("NamedColor") as text
253    set numIndexNo to 6 as integer
254  else if strColorSpaceName contains "Custom" then
255    set strSetColorName to ("Custom") as text
256    set numIndexNo to 6 as integer
257  else if strColorSpaceName contains "Pattern" then
258    set strSetColorName to ("Pattern") as text
259    set numIndexNo to 6 as integer
260  end if
261  #
262  set ocidTagName to (refMe's NSString's stringWithString:(strSetColorName))
263  (ocidTagNamesKeyArray's addObject:(ocidTagName))
264  #重複削除
265  set ocidArraySet to refMe's NSSet's setWithArray:(ocidTagNamesKeyArray)
266  set ocidSetTagNamesKeyArray to ocidArraySet's allObjects()
267  set listDone to (ocidFilePathURL's setResourceValue:(ocidSetTagNamesKeyArray) forKey:(refMe's NSURLTagNamesKey) |error| :(reference))
268  if (item 1 of listDone) is false then
269    return false
270  end if
271  #
272  set ocidLabelNo to refMe's NSNumber's numberWithInteger:(numIndexNo)
273  set listDone to (ocidFilePathURL's setResourceValue:(ocidLabelNo) forKey:(refMe's NSURLLabelNumberKey) |error| :(reference))
274  if (item 1 of listDone) is false then
275    return false
276  end if
277  
278  ###
279  try
280    log strFilePath
281  on error
282    #エラーになったらfalseを戻す
283    return false
284  end try
285  
286  return true
287end doAction
AppleScriptで生成しました

|

[Spotlight]カラープロファイルで検索


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#com.cocolog-nifty.quicktimer.icefloe
004#
005# Finder 前面のフォルダを カラースペースで検索します
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use framework "CoreGraphics"
011use scripting additions
012property refMe : a reference to current application
013
014
015####
016tell application "Finder"
017  set numCntWindow to (count of every Finder window) as integer
018  log numCntWindow
019  if numCntWindow > 0 then
020    try
021      set aliasDirPath to (target of front window) as alias
022    on error
023      log "通常のWindow以外を選択しましたのでデスクトップを検索します"
024      set aliasDirPath to (path to desktop folder from user domain) as alias
025    end try
026  else
027    log "Windowがありませんユーザーのデスクトップを検索します"
028    set aliasDirPath to (path to desktop folder from user domain) as alias
029  end if
030end tell
031set strDirPath to (POSIX path of aliasDirPath) as string
032if strDirPath contains "/Volumes/" then
033  set strDirPath to ("/System/Volumes/Data" & strDirPath) as string
034end if
035#パス
036set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath)
037set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
038set ocidDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath) isDirectory:(true)
039set strDirName to ocidDirPathURL's lastPathComponent() as text
040###
041
042set listColorSpace to {"Gray", "RGB", "CMYK", "LAB", "DeviceN", "Indexed"} as list
043###ダイアログ
044set strName to name of current application as text
045if strName is "osascript" then
046  tell application "Finder" to activate
047else
048  tell current application to activate
049end if
050set strTitle to ("選んでください") as text
051set strPrompt to ("検索するカラースペースを選んでください\nLAB DeviceN Indexedは無い場合がほとんどです") as text
052try
053  set listResponse to (choose from list listColorSpace with title strTitle with prompt strPrompt default items (item 1 of listColorSpace) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
054on error
055  return "エラーしました"
056end try
057if (item 1 of listResponse) is false then
058  return "キャンセルしましたA"
059else if (item 1 of listResponse) is "キャンセル" then
060  return "キャンセルしましたB"
061else
062  set strResponse to (item 1 of listResponse) as string
063end if
064####
065set ocidColorSpaceModelArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
066if strResponse is "Gray" then
067  ocidColorSpaceModelArray's addObject:(refMe's NSColorSpaceModelGray)
068else if strResponse is "RGB" then
069  ocidColorSpaceModelArray's addObject:(refMe's NSColorSpaceModelRGB)
070else if strResponse is "CMYK" then
071  ocidColorSpaceModelArray's addObject:(refMe's NSColorSpaceModelCMYK)
072else if strResponse is "LAB" then
073  ocidColorSpaceModelArray's addObject:(refMe's NSColorSpaceModelLAB)
074else if strResponse is "DeviceN" then
075  ocidColorSpaceModelArray's addObject:(refMe's NSColorSpaceModelDeviceN)
076else if strResponse is "Indexed" then
077  ocidColorSpaceModelArray's addObject:(refMe's NSColorSpaceModelIndexed)
078end if
079#
080set ocidColorSpaceArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
081repeat with itemColorModel in ocidColorSpaceModelArray
082  set ocidColorSpacesArray to (refMe's NSColorSpace's availableColorSpacesWithModel:(itemColorModel))
083  (ocidColorSpaceArray's addObjectsFromArray:(ocidColorSpacesArray))
084end repeat
085#
086set ocidColorSpaceNameArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
087repeat with itemColorSpace in ocidColorSpaceArray
088  set ocidColorSpageName to itemColorSpace's localizedName()
089  (ocidColorSpaceNameArray's addObject:(ocidColorSpageName))
090end repeat
091#
092set ocidArraySet to refMe's NSSet's setWithArray:(ocidColorSpaceNameArray)
093set ocidUniqueArray to ocidArraySet's allObjects()
094set ocidSortedUniqueArray to ocidUniqueArray's sortedArrayUsingSelector:("localizedStandardCompare:")
095set listColorSpaceName to ocidSortedUniqueArray as list
096
097set numCntArray to ocidSortedUniqueArray's |count|() as integer
098log numCntArray
099if numCntArray = 0 then
100  log "プロファイルがありませんでしたリロードします"
101  tell application "Finder"
102    set aliasPathToMe to (path to me) as alias
103  end tell
104  run script aliasPathToMe with parameters "プロファイルがありませんでしたリロードします"
105end if
106
107###ダイアログ
108set strName to name of current application as text
109if strName is "osascript" then
110  tell application "Finder" to activate
111else
112  tell current application to activate
113end if
114set strTitle to ("選んでください") as text
115set strPrompt to ("検索するプロファイルを選んでください") as text
116try
117  set listResponse to (choose from list listColorSpaceName with title strTitle with prompt strPrompt default items (item 1 of listColorSpaceName) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
118on error
119  return "エラーしましたC"
120end try
121if (item 1 of listResponse) is false then
122  return "キャンセルしましたA"
123else if (item 1 of listResponse) is "キャンセル" then
124  return "キャンセルしましたB"
125else
126  set strResponse to (item 1 of listResponse) as string
127end if
128
129####
130#APFS
131set listResponse to (ocidDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLFileContentIdentifierKey) |error| :(reference))
132set ocidScopeNo to (item 2 of listResponse)
133#それ以外
134if ocidScopeNo = (missing value) then
135  set listResponse to (ocidDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLFileIdentifierKey) |error| :(reference))
136  set ocidScopeNo to (item 2 of listResponse)
137end if
138####ルート
139set ocidRootDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
140ocidRootDict's setValue:(1 as integer) forKey:("CompatibleVersion")
141set strSetValue to ("(kMDItemProfileName = \"" & strResponse & "\"cd)") as string
142ocidRootDict's setValue:(strSetValue) forKey:("RawQuery")
143####RawQuery
144set ocidRawQueryDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
145ocidRawQueryDict's setValue:(false as boolean) forKey:("FinderFilesOnly")
146ocidRawQueryDict's setValue:(true as boolean) forKey:("UserFilesOnly")
147set strSetValue to ("(kMDItemProfileName = \"" & strResponse & "\"cd)") as string
148ocidRawQueryDict's setValue:(strSetValue) forKey:("RawQuery")
149if strDirPath contains "/Users/" then
150  set strSetValue to ("/System/Volumes/Data" & strDirPath) as string
151else if strDirPath contains "/Volumes/" then
152  set strSetValue to ("/System/Volumes/Data" & strDirPath) as string
153else
154  set strSetValue to (strDirPath) as string
155end if
156set listSetList to {(strSetValue)} as list
157ocidRawQueryDict's setObject:(listSetList) forKey:("SearchScopes")
158####SearchCriteria
159set ocidSearchCriteriaDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
160#
161ocidSearchCriteriaDict's setValue:(ocidScopeNo as integer) forKey:("FXScope")
162set strSetValue to (strDirPath) as string
163set listSetList to {(strDirPath)} as list
164ocidSearchCriteriaDict's setObject:(listSetList) forKey:("FXScopeArrayOfPaths")
165#
166set listSetList to {(strDirPath as string)} as list
167ocidSearchCriteriaDict's setValue:(listSetList) forKey:("CurrentFolderPath")
168#
169set ocidCriteriaDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
170ocidCriteriaDict's setValue:(0 as integer) forKey:("rowType")
171ocidCriteriaDict's setObject:({} as list) forKey:("subrows")
172set listSetList to {("kMDItemColorSpace" as string), (103 as integer), (104 as integer)} as list
173ocidCriteriaDict's setObject:(listSetList) forKey:("criteria")
174set listSetList to {("カラープロファイル" as string), ("contains" as string), (strResponse as string)} as list
175ocidCriteriaDict's setObject:(listSetList) forKey:("displayValues")
176ocidSearchCriteriaDict's setObject:({ocidCriteriaDict} as list) forKey:("FXCriteriaSlices")
177###
178ocidRootDict's setObject:(ocidRawQueryDict) forKey:("RawQueryDict")
179ocidRootDict's setObject:(ocidSearchCriteriaDict) forKey:("SearchCriteria")
180###作成したDICTをPLISTに
181set appSerialization to (refMe's NSPropertyListSerialization)
182set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
183set listResponse to appSerialization's dataWithPropertyList:(ocidRootDict) format:(ocidFormat) options:(0) |error| :(reference)
184set ocidPlistData to (item 1 of listResponse)
185###保存先を作成
186set appFileManager to refMe's NSFileManager's defaultManager()
187set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask))
188set ocidDocumentDirPathURL to ocidURLsArray's firstObject()
189set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("Saved Searches/プロファイル別") isDirectory:(true)
190#フォルダを作って
191set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
192ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
193set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)
194#保存ファイルパス
195set strFileName to (strDirName & "_" & strResponse) as text
196set ocidBasePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:(false)
197set ocidSaveFilePathURL to ocidBasePathURL's URLByAppendingPathExtension:("savedSearch")
198###保存
199set ocidOption to (refMe's NSDataWritingAtomic)
200set listDone to ocidPlistData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference)
201###
202set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
203set boolDone to appSharedWorkspace's openURL:(ocidSaveFilePathURL)
204
AppleScriptで生成しました

|

NSColorSpaceModel


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013
014log refMe's NSColorSpaceModelUnknown as integer
015log refMe's NSColorSpaceModelGray as integer
016log refMe's NSColorSpaceModelRGB as integer
017log refMe's NSColorSpaceModelCMYK as integer
018log refMe's NSColorSpaceModelLAB as integer
019log refMe's NSColorSpaceModelDeviceN as integer
020log refMe's NSColorSpaceModelIndexed as integer
021log refMe's NSColorSpaceModelPatterned as integer
022(*-1*)
023(*0*)
024(*1*)
025(*2*)
026(*3*)
027(*4*)
028(*5*)
029(*6*)
AppleScriptで生成しました

|

choose color NSCOLOR colorWithRed 戻し 小数点以下4位まで


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "UniformTypeIdentifiers"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013##############################
014#RGB16bitColorに16bitRGB値を格納
015tell current application
016  set strName to name as text
017end tell
018if strName is "osascript" then
019  tell application "Finder" to activate
020else
021  tell current application to activate
022end if
023#ダイアログ
024set the listRGB16bitColor to (choose color default color {65535, 55535, 0, 0}) as list
025log class of listRGB16bitColor
026log listRGB16bitColor as list
027
028##########Color Picker Value 16Bit
029set numRcolor16Bit to item 1 of listRGB16bitColor as number
030set numGcolor16Bit to item 2 of listRGB16bitColor as number
031set numBcolor16Bit to item 3 of listRGB16bitColor as number
032set numAcolor16Bit to 65535 as text
033
034set strRcolorFloat to doGetDecimal((numRcolor16Bit / 65535), 4) as text
035set strGcolorFloat to doGetDecimal((numGcolor16Bit / 65535), 4) as text
036set strBcolorFloat to doGetDecimal((numBcolor16Bit / 65535), 4) as text
037set strAcolorFloat to doGetDecimal((numAcolor16Bit / 65535), 4) as text
038
039set strDisptext to "colorWithRed:" & strRcolorFloat & " green:" & strGcolorFloat & " blue:" & strBcolorFloat & " alpha:" & strAcolorFloat & " " as text
040
041------ダイアログ表示用のテキスト
042set strRGB8bitColor to "R:" & strRcolorFloat & " G:" & strGcolorFloat & " B:" & strBcolorFloat as text
043##############################
044tell current application
045  set strName to name as text
046end tell
047if strName is "osascript" then
048  tell application "Finder" to activate
049else
050  tell current application to activate
051end if
052set aliasIconPath to POSIX file "/System/Applications/Utilities/Digital Color Meter.app/Contents/Resources/AppIcon.icns" as alias
053set strTitle to ("コードをコピーしてください") as text
054set strMes to ("" & strRGB8bitColor & " のNSCOLORが\n出来ました\nコピーして使って下さい。") as text
055set recordResult to (display dialog strMes default answer the strDisptext with icon aliasIconPath with title strTitle buttons {"クリップボードにコピー", "OK", "キャンセル"} default button "OK" cancel button "キャンセル" giving up after 20 without hidden answer) as record
056
057if button returned of recordResult is "クリップボードにコピー" then
058  try
059    set strText to text returned of recordResult as text
060    ####ペーストボード宣言
061    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
062    set ocidText to (refMe's NSString's stringWithString:(strText))
063    appPasteboard's clearContents()
064    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
065  on error
066    tell application "Finder"
067      set the clipboard to strTitle as text
068    end tell
069  end try
070end if
071
072############################
073# 小数点以下X位で丸める
074# argTextNo テキスト形式の数値
075# argDigits 桁数
076############################
077to doGetDecimal(argTextNo, argDigits)
078  set strTextNo to argTextNo as text
079  set ocidTextNo to refMe's NSDecimalNumber's alloc()'s initWithString:(strTextNo)
080  #小数点以下切り上げ
081  set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
082  ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundCeiling)
083  ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
084  ocidFormatter's setMinimumFractionDigits:(1)
085  ocidFormatter's setMaximumFractionDigits:(argDigits)
086  set ocidRoundCeiling to ocidFormatter's stringFromNumber:(ocidTextNo)
087  #テキストで戻す
088  set strDecimalNo to ocidRoundCeiling as text
089  #set numPtSize to strPtSize as number
090  return strDecimalNo
091  
092end doGetDecimal
AppleScriptで生成しました

|

画像のカラースペースを取得


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application


############################
### ダイアログ
############################
set appFileManager to refMe's NSFileManager's defaultManager()
###デフォルトロケーション
set ocidUserDocumentPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopPathURL to ocidUserDocumentPathArray's firstObject()
set aliasDefaultLocation to (ocidDesktopPathURL's absoluteURL()) as alias
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
####ダイアログを出す
set listUTI to {"public.image"} as list
set listAliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles, showing package contents and multiple selections allowed) as list

############################
####選んだファイルパスの数だけ繰り返し
############################
repeat with itemAliasFilePath in listAliasFilePath
  ##入力パス
  set strFilePath to (POSIX path of itemAliasFilePath) as text
  set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
  ###########################
  ###【1】元イメージ
  ###読み込むイメージURL
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
  ##NSIMAGEに読み込む
  set ocidReadImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidFilePathURL))
  ##BitMapRepに変換
  set ocidReadImageRepArray to ocidReadImage's representations()
  set ocidReadImageRep to (ocidReadImageRepArray's objectAtIndex:0)
  ##ピクセルサイズ取得
  set ocidColorSpace to ocidReadImageRep's colorSpace()
  set ocidColorSpaceName to ocidReadImageRep's colorSpaceName()
log ocidColorSpaceName as text
  #
  set strColorName to ocidColorSpace's localizedName as text
  set numColorNO to ocidColorSpace's colorSpaceModel as integer
log strColorName
log numColorNO
  (*
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-38db59.html
  
NSColorSpaceModelUnknown(0): 未知のカラーモデル
NSColorSpaceModelGray(1): グレースケールカラーモデル
NSColorSpaceModelRGB(3): RGBカラーモデル
NSColorSpaceModelCMYK(4): CMYKカラーモデル
NSColorSpaceModelLAB(5): CIELABカラーモデル
NSColorSpaceModelDeviceN(6): デバイスNカラーモデル
NSColorSpaceModelIndexed(7): インデックスカラーモデル
NSColorSpaceModelPattern(8): パターンカラーモデル
*)
  
  set ocidReadImage to ""
  set ocidReadImageRepArray to ""
  set ocidReadImageRep to ""
end repeat


|

NSColorSpace


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"

property refMe : a reference to current application
  (*
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-38db59.html
  
NSColorSpaceModelUnknown(0): 未知のカラーモデル
NSColorSpaceModelGray(1): グレースケールカラーモデル
NSColorSpaceModelRGB(3): RGBカラーモデル
NSColorSpaceModelCMYK(4): CMYKカラーモデル
NSColorSpaceModelLAB(5): CIELABカラーモデル
NSColorSpaceModelDeviceN(6): デバイスNカラーモデル
NSColorSpaceModelIndexed(7): インデックスカラーモデル
NSColorSpaceModelPattern(8): パターンカラーモデル
*)

set ocidColor to refMe's NSColorSpace's deviceGrayColorSpace
log ocidColor's numberOfColorComponents as integer
(*1*)

set ocidColor to refMe's NSColorSpace's genericGrayColorSpace
log ocidColor's numberOfColorComponents as integer
(*1*)

set ocidColor to refMe's NSColorSpace's genericGamma22GrayColorSpace
log ocidColor's numberOfColorComponents as integer
(*1*)

set ocidColor to refMe's NSColorSpace's extendedGenericGamma22GrayColorSpace
log ocidColor's numberOfColorComponents as integer
(*1*)

set ocidColor to refMe's NSColorSpace's genericRGBColorSpace
log ocidColor's numberOfColorComponents as integer
(*3*)

set ocidColor to refMe's NSColorSpace's deviceRGBColorSpace
log ocidColor's numberOfColorComponents as integer
(*3*)

set ocidColor to refMe's NSColorSpace's sRGBColorSpace
log ocidColor's numberOfColorComponents as integer
(*3*)

set ocidColor to refMe's NSColorSpace's extendedSRGBColorSpace
log ocidColor's numberOfColorComponents as integer
(*3*)

set ocidColor to refMe's NSColorSpace's displayP3ColorSpace
log ocidColor's numberOfColorComponents as integer
(*3*)

set ocidColor to refMe's NSColorSpace's adobeRGB1998ColorSpace
log ocidColor's numberOfColorComponents as integer
(*3*)

set ocidColor to refMe's NSColorSpace's deviceCMYKColorSpace
log ocidColor's numberOfColorComponents as integer
(*4*)

set ocidColor to refMe's NSColorSpace's genericCMYKColorSpace
log ocidColor's numberOfColorComponents as integer
(*4*)

|

ICCプロファイルを読み込む

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.6"
use framework "Foundation"
use framework "AppKit"
use scripting additions


property refMe : a reference to current application


set strIccFilePath to "/System/Library/ColorSync/Profiles/sRGB Profile.icc"
set ocidIccFilePathStr to (refMe's NSString's stringWithString:strIccFilePath)
set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath
set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidIccFilePath isDirectory:false)
###NSDATAに読み込む
set ocidReadProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:ocidFilePathURL

###カラースペース
set ocidColorSpace to refMe's NSColorSpace's alloc()'s initWithICCProfileData:ocidReadProfileData

|

[NSCloro]色を指定する

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application

##色名
set ocidSetColor to refMe's NSColor's greenColor()
log ocidSetColor
log className() of ocidSetColor as text
log ocidSetColor's redComponent()
log ocidSetColor's greenComponent()
log ocidSetColor's blueComponent()
log ocidSetColor's alphaComponent()
(*
blackColor()
blueColor()
brownColor()
cyanColor()
grayColor()
greenColor()
lightGrayColor()
magentaColor()
orangeColor()
purpleColor()
redColor()
whiteColor()
yellowColor()
*)

##白黒
set ocidSetColor to refMe's NSColor's colorWithWhite:1.0 alpha:1.0
log ocidSetColor
log className() of ocidSetColor as text
log ocidSetColor
log className() of ocidSetColor as text
log ocidSetColor's whiteComponent()
log ocidSetColor's alphaComponent()
##CMYK指定の場合の場合はこちら
set ocidSetColor to refMe's NSColor's colorWithDeviceCyan:1.0 magenta:0 yellow:0 black:0 alpha:1.0
log ocidSetColor
log className() of ocidSetColor as text
log ocidSetColor's cyanComponent()
log ocidSetColor's magentaComponent()
log ocidSetColor's yellowComponent()
log ocidSetColor's blackComponent()
log ocidSetColor's alphaComponent()
###RGB系 DeviceRGB
set ocidSetColor to refMe's NSColor's colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0
log ocidSetColor
log className() of ocidSetColor as text
log ocidSetColor's redComponent()
log ocidSetColor's greenComponent()
log ocidSetColor's blueComponent()
log ocidSetColor's alphaComponent()

###RGB系 DisplayP3
set ocidSetColor to refMe's NSColor's colorWithDisplayP3Red:1.0 green:0.0 blue:0.0 alpha:1.0
log ocidSetColor
log className() of ocidSetColor as text
log ocidSetColor's redComponent()
log ocidSetColor's greenComponent()
log ocidSetColor's blueComponent()
log ocidSetColor's alphaComponent()

### Hue
set ocidSetColor to refMe's NSColor's colorWithHue:1.0 saturation:0.0 brightness:0.0 alpha:1.0
log ocidSetColor
log className() of ocidSetColor as text
log ocidSetColor's hueComponent()
log ocidSetColor's saturationComponent()
log ocidSetColor's brightnessComponent()
log ocidSetColor's alphaComponent()

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom