« 画像ファイルを2in1 2upにマージする(読み方向;右から左) 修正 | トップページ | 画像ファイルを2in1 2upにマージする(読み方向;上から下) 修正 »

解像度変更 修正


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

#!/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 ocidImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidFilePathURL))
    #BmpRepに変換
    set ocidImageRepArray to ocidImage's representations()
    set ocidImageRep to ocidImageRepArray's firstObject()
    set ocidSize to ocidImageRep's |size|()
    set numWptR to ocidSize's width
    set numHptR to ocidSize's height
    #プロパティを保存時用に取得
    try
      ##ちょいちょいpropertiesの取得でエラーになるので
      set ocidPropertiesDict to ocidImageRep's |properties|
    on error
      ##プロパティがある項目だけ保存用に取得する
      set ocidPropertiesDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
      set listProperties to {(refMe's NSImageColorSyncProfileData), (refMe's NSImageCompressionFactor), (refMe's NSImageCompressionMethod), (refMe's NSImageCurrentFrame), (refMe's NSImageCurrentFrameDuration), (refMe's NSImageDitherTransparency), (refMe's NSImageEXIFData), (refMe's NSImageFallbackBackgroundColor), (refMe's NSImageFrameCount), (refMe's NSImageGamma), (refMe's NSImageInterlaced), (refMe's NSImageLoopCount), (refMe's NSImageProgressive), (refMe's NSImageRGBColorTable)} as list
      repeat with itemPropertie in listProperties
        set ocidSetValue to (ocidImageRep's valueForProperty:(itemPropertie))
        if ocidSetValue ≠ (missing value) then
(ocidPropertiesDict's setObject:(ocidSetValue) forKey:(itemPropertie))
        end if
      end repeat
    end try
    #ピクセルサイズを取得
    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
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypePNG)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set ocidSaveFilePathURL to ocidFilePathURL
    else if strExtensionName is "jpg" then
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypeJPEG)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set ocidSaveFilePathURL to ocidFilePathURL
    else if strExtensionName is "jpeg" then
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypeJPEG)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set ocidSaveFilePathURL to ocidFilePathURL
    else if strExtensionName is "tif" then
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypeTIFF)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set ocidSaveFilePathURL to ocidFilePathURL
    else if strExtensionName is "tiff" then
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypeTIFF)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set ocidSaveFilePathURL to ocidFilePathURL
      (*
else if strExtensionName is "bmp" then
set ocidSaveImageType to (refMe's NSBitmapImageFileTypeBMP)
set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
set ocidSaveFilePathURL to ocidFilePathURL
else if strExtensionName is "gif" then
set ocidSaveImageType to (refMe's NSBitmapImageFileTypeGIF)
set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
set ocidSaveFilePathURL to ocidFilePathURL
*)
    else
log "上記以外の場合はPNGで別名保存"
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypePNG)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set strSaveFileName to (strPrefixName & ".png") as text
      set ocidSaveFilePathURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strSaveFileName))
    end if
    ###上書き保存
    set boolResults to (ocidNewImageData's writeToURL:(ocidSaveFilePathURL) 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


|

« 画像ファイルを2in1 2upにマージする(読み方向;右から左) 修正 | トップページ | 画像ファイルを2in1 2upにマージする(読み方向;上から下) 修正 »

AppleScript NSBitmapImageRep」カテゴリの記事