« バージョンチェッカーv3 MacAdminのRSSを使用しない方法 | トップページ | [bash]DWViewerLightインストール »

画像の解像度だけ変更する


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

#!/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 "Quartz"
use framework "CoreImage"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

on run
  
  set aliasIconPass to (POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns") as alias
  set strDialogText to "ドロップしても利用できます"
  set strTitleText to "画像ファイルを選んでください"
  set listButton to {"ファイルを選びます", "キャンセル"} as list
display dialog strDialogText buttons listButton default button 1 cancel button 2 with title strTitleText with icon aliasIconPass giving up after 2 with hidden answer
  
  set aliasDefaultLocation to (path to desktop folder from user domain) as alias
  set listChooseFileUTI to {"public.png", "public.jpeg"}
  set strPromptText to "イメージファイルを選んでください" as text
  set strPromptMes to "イメージファイルを選んでください" as text
  set listAliasFilePath to (choose file strPromptMes with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with showing package contents, invisibles and multiple selections allowed) as list
  
  -->値をOpenに渡たす
open listAliasFilePath
end run


on open listAliasFilePath
  ##########################
  ####解像度を指定する
  ##########################
  ###ダイアログを前面に出す
  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 listResolution to {"72", "96", "120", "144", "216", "288", "300", "360"} as list
  #
  try
    set listResponse to (choose from list listResolution with title "選んでください" with prompt "解像度を選んでください" default items (item 1 of listResolution) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
  on error
log "エラーしました"
return "エラーしました"
  end try
  if (item 1 of listResponse) is false then
return "キャンセルしました"
  end if
  set strResolution to (item 1 of listResponse) as text
  set numResolution to strResolution as integer
  
  ##########################
  ####ファイルの数だけ繰り返し
  ##########################
  set ocidFalse to (refMe's NSNumber's numberWithBool:false)'s boolValue
  set ocidTrue to (refMe's NSNumber's numberWithBool:true)'s boolValue
  repeat with itemAliasFilePath in listAliasFilePath
    ####まずはUNIXパスにして
    set strFilePath to (POSIX path of itemAliasFilePath) as text
    set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
    set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
    set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
    ####ファイル名を取得
    set ocidFileName to ocidFilePathURL's lastPathComponent()
    ####ファイル名から拡張子を取っていわゆるベースファイル名を取得
    set strPrefixName to ocidFileName's stringByDeletingPathExtension as text
    ####コンテナディレクトリを取得
    set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
    ###拡張子を取得して小文字にしておく
    set ocidExtensionName to ocidFilePathURL's pathExtension()
    set ocidExtensionNameLowCase to ocidExtensionName's lowercaseString()
    set strExtensionName to ocidExtensionNameLowCase as text
    #####################
    #### 本処理
    #####################
    ####データ読み込み
    set ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:ocidFilePathURL)
    set ocidImageDataSize to ocidImageData's |size|()
    set numPointWidth to width of ocidImageDataSize
    set numPointHeight to height of ocidImageDataSize
    ####ImageRepに変換
    set ocidImageRepArray to ocidImageData's representations()
    ####サイズと解像度計算
    set ocidImageRep to (ocidImageRepArray's objectAtIndex:0)
    #プロパティを保存時用に取得
    set ocidPropertiesArray to ocidImageRep's |properties|
    #ピクセルサイズを取得
    set numPixelsWidth to ocidImageRep's pixelsWide()
    set numPixelsHeight to ocidImageRep's pixelsHigh()
    #サイズセットする比率
    set numSetResolution to (numResolution / 72.0) as number
    # ピクセルサイズに↑の比率で割って セットするポイントサイズ
    set numNewImageWidth to (numPixelsWidth / numSetResolution) as number
    set numNewImageHeight to (numPixelsHeight / numSetResolution) as number
    #####リサイズの値レコード
    set recordNewImageSize to {width:numNewImageWidth, height:numNewImageHeight} as record
    ###リサイズ
(ocidImageRep's setSize:(recordNewImageSize))
    #拡張子による分岐
    if strExtensionName is "png" then
      ###展開 PNG
      set ocidSaveImageType to refMe's NSBitmapImageFileTypePNG
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesArray))
    else if strExtensionName is "jpg" then
      ###展開 JPEG
      set ocidSaveImageType to refMe's NSBitmapImageFileTypeJPEG
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesArray))
    else if strExtensionName is "jpeg" then
      set ocidSaveImageType to refMe's NSBitmapImageFileTypeJPEG
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesArray))
    else
log "処理しないでそのまま保存"
    end if
    ###上書き保存
    set boolResults to (ocidNewImageData's writeToURL:ocidFilePathURL atomically:true)
    #結果
    if boolResults is true then
log "処理OK"
    else
log "処理NGなのでそのままにする"
log "失敗ラベル赤を塗る"
      set boolResults to (ocidImagFilePathURL's setResourceValue:6 forKey:(refMe's NSURLLabelNumberKey) |error|:(reference))
    end if
    set ocidImageData to ""
    set ocidImageRep to ""
    set ocidNewImageData to ""
  end repeat
end open


|

« バージョンチェッカーv3 MacAdminのRSSを使用しない方法 | トップページ | [bash]DWViewerLightインストール »

AppleScript NSImage」カテゴリの記事