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

カテゴリー: Swift

Finder上で選択中のHEIC画像から位置情報を削除_v5

Posted on 3月 17 by Takaaki Naganoya

Finder上で選択中のHEIC画像から位置情報(緯度、経度)を削除するAppleScriptです。

JEPGのようにEXIF情報を編集するのとはHEIC画像の構造が異なるため、方法については試行錯誤の最中です。いろいろ試してみたところ、HEICの解析を行なっても仕様を追いきれなかったので、メモリ上にHEIC画像を読み込んで、HEIC画像でファイルに書き出し直しています。

また、画像の回転状態を元画像から取得して、自動回転させ、元画像と回転状態を維持しています。

NSImageにHEIC画像を読み込んでファイル書き込みするという方法でも、問題なく処理できることでしょう。

AppleScript名:Finder上で選択中のHEIC画像から位置情報を削除_v5.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2026/03/16
—
–  Copyright © 2026 Piyomaru Software, All Rights Reserved
—

on run
  tell application "Finder"
    set targetFilesList to selection as alias list
  end tell
  
  
if targetFilesList is {} then
    display alert "エラー" message "FinderでHEICファイルを選択してください。"
    
return
  end if
  
  
set successfulReconstructions to 0
  
  
repeat with currentFile in targetFilesList
    set posixPathString to POSIX path of currentFile
    
    
if (posixPathString ends with ".heic") or (posixPathString ends with ".HEIC") then
      set isProcessed to my redrawImageMaintainingOrientation(posixPathString)
      
if isProcessed then
        set successfulReconstructions to successfulReconstructions + 1
      end if
    end if
  end repeat
  
  
display notification (successfulReconstructions as string) & " 個のファイルの位置情報を削除しました。" with title "位置情報抹消完了"
end run

on redrawImageMaintainingOrientation(posixPath)
  — Swiftプログラム
  
— 1. 画像データ(CGImage)と回転情報のみを抽出
  
— 2. 新しい画像として書き出し、回転情報を再設定
  
— 3. GPS情報は一切含めない
  
set swiftSource to "
import Foundation
import ImageIO
import CoreGraphics

let path = \"" & posixPath & "\"
let url = URL(fileURLWithPath: path)

guard let source = CGImageSourceCreateWithURL(url as CFURL, nil),
let type = CGImageSourceGetType(source) else { exit(1) }

let count = CGImageSourceGetCount(source)
// 元のファイルに新規作成(上書き)としてDestinationを準備
guard let destination = CGImageDestinationCreateWithURL(url as CFURL, type, count, nil) else { exit(1) }

for i in 0..<count {
// 1. 画像データのみを抽出
if let cgImage = CGImageSourceCreateImageAtIndex(source, i, nil) {

// 2. 新しいプロパティを作成(空の辞書)
let freshProperties = NSMutableDictionary()

// 3. 元のメタデータから「回転情報」だけを取得して引き継ぐ
if let oldProps = CGImageSourceCopyPropertiesAtIndex(source, i, nil) as? [String: Any] {
// 回転情報 (kCGImagePropertyOrientation)
if let orientation = oldProps[kCGImagePropertyOrientation as String] {
freshProperties[kCGImagePropertyOrientation as String] = orientation
}

// カラープロファイル (ColorModel) も維持しないと色が変わるため引き継ぐ
if let colorModel = oldProps[kCGImagePropertyColorModel as String] {
freshProperties[kCGImagePropertyColorModel as String] = colorModel
}
}

// 4. 回転情報は維持し、GPSを含まないプロパティで画像を書き込む
CGImageDestinationAddImage(destination, cgImage, freshProperties as CFDictionary)
}
}

if CGImageDestinationFinalize(destination) {
exit(0)
} else {
exit(1)
}
"

  try
    do shell script "swift -e " & quoted form of swiftSource
    
return true
  on error
    return false
  end try
end redrawImageMaintainingOrientation

★Click Here to Open This Script 

Posted in geolocation Image Swift | Tagged 26.0savvy | Leave a comment

住所ジオコーダー2026年最新版

Posted on 3月 16 by Takaaki Naganoya

AppleScriptから住所ジオコーダー/逆住所ジオコーダーの機能にアクセスして日常的に利用していますが、セキュリティ機能の強化によって旧来の機能にアクセスできなくなりつつあります。

住所ジオコーダー系の機能については、さまざまな方法でアクセスできるようにしてきましたが、どうやらmacOS 27あたりでさらにセキュリティ縛りがきつくなることが想定されます。

さまざまな方法を検討した結果、

・SwiftでCLIツールを作ってAppleScriptバンドル内に入れてdo shell scriptコマンドで呼び出す

これは実際にNFCタグの操作Scriptを作った際に試した方法です。問題はないのですが、これだと実行できないAppleScriptの実行環境もあるので、なるべく1つのAppleScript内で完結したいところです。

・AppleScript内に文字列でSwiftのプログラムを記述し、do shell scriptコマンド経由で「swift」(/usr/bin/swift)を呼び出して実行させる

今回はこれを採用してみました。AppleScript実行プログラム側のセキュリティ設定次第ですが、環境を選ばずに実行できるはずです。なお、Script Debugger上では動作しません。

AppleScript名:住所から緯度、経度を返す(Swift版).scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2026/03/16
—
–  Copyright © 2026 Piyomaru Software, All Rights Reserved
—

set myAddress to "東京都新宿区西新宿2丁目8−1"

set gRes to my geocode(myAddress)
set {aLat, Along} to parseByDelim(gRes, "|") of me
–> {"35.6894863", "139.6917052"}

on geocode(addressString)
  set swiftSource to "
import CoreLocation
import Foundation

let address = \"$TARGET$\"
let geocoder = CLGeocoder()
var completed = false
var resultString = \"Error: Timeout or Access Denied\"

geocoder.geocodeAddressString(address) { placemarks, error in
if let loc = placemarks?.first?.location {
resultString = \"\\(loc.coordinate.latitude)|\\(loc.coordinate.longitude)\"
} else {
resultString = \"Error: \\(error?.localizedDescription ?? \"Unknown\")\"
}
completed = true
}

// 5秒間、結果を待機しながらランループを回す
let timeout = Date(timeIntervalSinceNow: 5.0)
while !completed && Date() < timeout {
RunLoop.current.run(mode: .default, before: Date(timeIntervalSinceNow: 0.00001))
}

print(resultString)
"

  return my runSwift(swiftSource, addressString)
end geocode

on runSwift(source, targetValue)
  set finalCode to my replaceText("$TARGET$", targetValue, source)
  
try
    — ヒアドキュメントで実行
    
return do shell script "swift – <<’EOF’" & return & finalCode & return & "EOF"
  on error errMsg
    return "実行エラー: " & errMsg
  end try
end runSwift

on replaceText(s, r, t)
  set AppleScript’s text item delimiters to s
  
set textItems to every text item of t
  
set AppleScript’s text item delimiters to r
  
set res to textItems as string
  
set AppleScript’s text item delimiters to ""
  
return res
end replaceText

on parseByDelim(aData, aDelim)
  set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set dList to text items of aData
  
set AppleScript’s text item delimiters to curDelim
  
return dList
end parseByDelim

★Click Here to Open This Script 

AppleScript名:緯度経度から住所を返す(Swift版).scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2026/03/16
—
–  Copyright © 2026 Piyomaru Software, All Rights Reserved
—

— サンプル座標(東京都庁付近)
set myCoords to "35.6894863|139.6917052"
set gRes to my reverseGeocode(myCoords)
–> "東京都新宿区東京都庁"

———————————————————
— ハンドラ: 緯度|経度 -> 住所
———————————————————
on reverseGeocode(latLonString)
  set swiftSource to "
import CoreLocation
import Foundation

let input = \"$TARGET$\"
let parts = input.components(separatedBy: \"|\")

guard parts.count == 2,
let lat = Double(parts[0].trimmingCharacters(in: .whitespaces)),
let lon = Double(parts[1].trimmingCharacters(in: .whitespaces))
else {
print(\"Error: Invalid input format\")
exit(1)
}

let location = CLLocation(latitude: lat, longitude: lon)
let geocoder = CLGeocoder()
var completed = false
var resultString = \"Error: Timeout or No Data\"

geocoder.reverseGeocodeLocation(location) { placemarks, error in
if let p = placemarks?.first {
// 都道府県 + 市区町村 + 地番・ビル名などを結合
let addr = \"\\(p.administrativeArea ?? \"\")\\(p.locality ?? \"\")\\(p.name ?? \"\")\"
resultString = addr
} else {
resultString = \"Error: \\(error?.localizedDescription ?? \"Unknown\")\"
}
completed = true
}

// 5秒間、結果が返るまでランループを回す
let timeout = Date(timeIntervalSinceNow: 5.0)
while !completed && Date() < timeout {
RunLoop.current.run(mode: .default, before: Date(timeIntervalSinceNow: 0.00001))
}

print(resultString)
"

  return my runSwift(swiftSource, latLonString)
end reverseGeocode

———————————————————
— Swift実行サブシステム
———————————————————
on runSwift(source, targetValue)
  set finalCode to my replaceText("$TARGET$", targetValue, source)
  
try
    — ファイルを作らず標準入力から実行
    
return do shell script "swift – <<’EOF’" & return & finalCode & return & "EOF"
  on error errMsg
    return "Error: " & errMsg
  end try
end runSwift

on replaceText(s, r, t)
  set AppleScript’s text item delimiters to s
  
set textItems to every text item of t
  
set AppleScript’s text item delimiters to r
  
set res to textItems as string
  
set AppleScript’s text item delimiters to ""
  
return res
end replaceText

★Click Here to Open This Script 

Posted in geolocation Swift | Tagged 26.0savvy | Leave a comment

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • macOS 26, Tahoe
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行
  • Script Debuggerがフリーダウンロードで提供されることに
  • AppleScript Dropletのバグっぽい動作が「復活」(macOS 15.5β)
  • macOS 26, 15.5でShortcuts.app「AppleScriptを実行」アクションのバグが修正される
  • Numbersで選択範囲のdateの年を+1する
  • Appleに買収されたPixelmator ProがAppleとしての初アップデート
  • Dock Menu
  • Applicationのactivateを記録する v2
  • シンプルな文字置換
  • macOS 15.5beta5(24F74)でaliasのキャスティングバグが修正された???
  • Claris FileMaker Pro 2025(v22)がリリースされた
  • Apple Creator Studioに含まれるKeynote/Pages/Numbersは新バージョン?
  • macOS 15.7.2 スクリプトメニューから実行できなくなった地図系ライブラリ?
  • 画像をAppleScriptでアスキーアート化
  • NaturalLanguage.frameworkを用いて日本語テキストの形態素解析を行う
  • macOS(Mac OS X/OS X)上のAppleScriptの歴史
  • Adobe InDesignのAppleScript実行エンジンがCarbonからCocoaベースに書き換えられる
  • 開始時刻から終了時刻までhh:mm形式の文字列を15分単位でリスト出力

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (439) 11.0savvy (283) 12.0savvy (212) 13.0savvy (204) 14.0savvy (159) 15.0savvy (170) 26.0savvy (45) CotEditor (68) Finder (53) Keynote (122) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (80) Pages (58) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • 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
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Newt On Project
  • NFC
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • 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
  • Scripting Additions
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • Swift
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2026年3月
  • 2026年2月
  • 2026年1月
  • 2025年12月
  • 2025年11月
  • 2025年10月
  • 2025年9月
  • 2025年8月
  • 2025年7月
  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 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