AutoHotkey 语言 创建自动整理手机照片地理位置脚本方案

AutoHotkey阿木 发布于 1 天前 2 次阅读


AutoHotkey Language Script for Organizing Mobile Photos with Geotags

Introduction

AutoHotkey is a powerful scripting language for automating tasks on Windows systems. It allows users to create scripts that can automate repetitive tasks, such as organizing files, managing windows, and more. In this article, we will explore how to create an AutoHotkey script to automatically organize mobile photos based on their geotags. Geotags are metadata that include the latitude and longitude coordinates of where a photo was taken, which can be very useful for organizing and searching for photos based on location.

Prerequisites

Before we dive into the script, ensure you have the following prerequisites:

1. AutoHotkey installed on your Windows system.
2. Access to the photos you want to organize, ideally in a folder structure that you can script to.
3. A basic understanding of AutoHotkey syntax and functions.

Script Overview

The script will perform the following steps:

1. Scans a specified directory for new photos.
2. Extracts geotags from the photos.
3. Creates a new folder structure based on the geotags.
4. Moves the photos into the appropriate folders.

Step 1: Scanning for New Photos

To scan for new photos, we'll use the `FileGetTime` function to check the last modified time of the files in the specified directory.

ahk
Persistent
SingleInstance, Force

; Set the directory to scan for new photos
scanDirectory := "C:PathToYourPhotos"

; Loop through the files in the directory
Loop, Files, %scanDirectory%., 2
{
; Get the last modified time of the file
fileTime := A_LoopLastModified

; Check if the file is new (modified within the last hour)
if (A_Now - fileTime < 3600)
{
; Process the file (extract geotags, create folders, move file)
ProcessFile(A_LoopFileLongPath)
}
}

Step 2: Extracting Geotags

To extract geotags, we'll use the `ImageMagick` command-line tool, which can read metadata from images. Ensure you have ImageMagick installed and the `convert` command available in your system's PATH.

ahk
ProcessFile(file)
{
; Extract geotags using ImageMagick
Run, convert "%file%" info:exif, , Hide

; Wait for the command to complete
WinWaitActive, ahk_class 32770

; Read the geotags from the output
geotags := Clipboard

; Close the output window
WinClose, ahk_class 32770

; Process the geotags
ProcessGeotags(file, geotags)
}

Step 3: Creating Folder Structure

Based on the geotags, we'll create a new folder structure. For simplicity, let's assume the geotags are in the format "Latitude, Longitude".

ahk
ProcessGeotags(file, geotags)
{
; Split the geotags by comma
SplitString, geotags, %geotags%, Comma

; Extract latitude and longitude
latitude := SubStr(geotags, 1, InStr(geotags, ",") - 1)
longitude := SubStr(geotags, InStr(geotags, ",") + 1)

; Create a folder path based on the geotags
folderPath := "C:PathToOrganizedPhotos" latitude "" longitude

; Create the folder structure if it doesn't exist
IfNotExist, %folderPath%
{
Loop, Parse, folderPath,
{
IfNotExist, %A_LoopField%
{
FileCreateDir, %A_LoopField%
}
}
}

; Move the file to the new folder
FileMove, %file%, %folderPath%, 1
}

Step 4: Final Touches

Now that we have the basic script working, let's add some final touches:

1. Add error handling to manage any issues that arise during the script's execution.
2. Allow the user to specify the directory to scan for new photos.
3. Provide feedback to the user on the script's progress.

ahk
Persistent
SingleInstance, Force

; Set the directory to scan for new photos
scanDirectory := "C:PathToYourPhotos"

; Loop through the files in the directory
Loop, Files, %scanDirectory%., 2
{
; Get the last modified time of the file
fileTime := A_LoopLastModified

; Check if the file is new (modified within the last hour)
if (A_Now - fileTime < 3600)
{
; Process the file (extract geotags, create folders, move file)
ProcessFile(A_LoopFileLongPath)
}
}

; Function to process the file
ProcessFile(file)
{
; ... (existing code)
}

; Function to process geotags
ProcessGeotags(file, geotags)
{
; ... (existing code)
}

; Function to provide feedback
ProvideFeedback(message)
{
MsgBox, %message%
}

Conclusion

In this article, we've explored how to create an AutoHotkey script to automatically organize mobile photos based on their geotags. By following the steps outlined above, you can create a script that scans a directory for new photos, extracts geotags, creates a folder structure based on those geotags, and moves the photos into the appropriate folders.

Remember to customize the script to fit your specific needs, such as the directory to scan and the format of your geotags. With a bit of tweaking, this script can become a valuable tool for organizing your mobile photos.