Applescript在多个文件夹中查找最接近给定时间创建的文件

时间:2019-01-24 01:51:52

标签: applescript

我正在尝试编写一个脚本,该脚本将搜索100个文件夹,并在每个文件夹中找到最接近上午9点创建的文件。

我正在进行30年的延时拍摄(已经4年了),我希望能够快速高效地找到最接近一天中特定时间创建的照片。

任何建议都将不胜感激。

我已经开始用applescript编写代码,但是我不知道如何比较“创建日期”

1 个答案:

答案 0 :(得分:1)

我认为尝试在 applescript 中执行此操作相当糟糕,所以我在bash中完成了该操作,无论如何,该版本已安装在所有Mac上。

您也可以使用do shell script applescript 调用我的脚本。

因此,您将以下内容保存为HOME目录中的NearestBirth

#!/bin/bash
################################################################################
# NearestBirth
# Mark Setchell
#
# Usage:
# NearestBirth DIRECTORY H:M:S
# Finds the file in the specified directory with the birth time nearest the 
# specified time.
################################################################################

# Set DEBUG=1 for verbose output, set DEBUG=0 for quiet
DEBUG=1

# Check we have 2 parameters and pick them up
if [ $# -ne 2 ]; then
   echo "Usage:"
   echo "NearestBirth DIRECTORY H:M:S"
   exit 1
fi

# Pick up parameters
dir=$1
hms=$2

[ $DEBUG -eq 1 ] && echo "DEBUG: dir=$dir, hms=$hms"

################################################################################
# hms2s - convert HH:MM:SS to seconds since midnight
################################################################################
hms2s(){
   IFS=: read h m s <<< "$1"
   ((result=(10#$h*3600)+(10#$m*60)+10#$s))
   echo $result
}

################################################################################
# birthtime - get birthtime of file in seconds since midnight on day of creation
################################################################################
birthtime(){
   # The following command gives the birthtime of a file on macOS
   # stat -f "%SB" someFile.txt
   # Feb  4 18:49:05 2019
   s=$(stat -f "%SB" "$1" | awk '{print $3}')
   result=$(hms2s $s)
   echo $result
}

################################################################################
# main
################################################################################
cd "$top" || { echo "Unable to change to $top"; exit 1; }

# Work out target age of file in seconds since midnight
tgt=$(hms2s "$hms")
[ $DEBUG -eq 1 ] && echo "DEBUG: Target age in seconds=$tgt"

shopt -s nullglob

nearestTime=100000                # More than number of seconds in a day - must surely be beaten
nearestFile="ERROR: None found"   # Must surely get overwritten

# Iterate over all files in the directory
for f in *; do
   birth=$(birthtime "$f")
   ((diff=tgt-birth))
   [ $diff -lt 0 ] && ((diff=-1*diff))
   if [ $diff -lt $nearestTime ] ; then
       nearestTime=$diff
       nearestFile="$f"
       [ $DEBUG -eq 1 ] && echo "DEBUG: New nearest ($birth): $f"
   fi
done
echo "$nearestFile"

然后,您将启动终端,并使用以下命令使脚本可执行:

chmod +x $HOME/NearestBirth

然后,您可以像这样运行脚本,以在目录/Users/mark/StackOverflow中找到最接近09:00:00的文件:

$HOME/NearestBirth  "/Users/mark/StackOverflow"  09:00:00

示例输出(DEBUG = 1)

./NearestBirth /Users/mark/StackOverflow 09:00:00
DEBUG: dir=/Users/mark/StackOverflow, hms=09:00:00
DEBUG: Target age in seconds=32400
DEBUG: New nearest (64051): 16bit.py
DEBUG: New nearest (60948): 2d-plot
DEBUG: New nearest (46205): 45.py
DEBUG: New nearest (26224): 565.py
DEBUG: New nearest (38203): HSV.py
DEBUG: New nearest (32131): IMBoxAreas
DEBUG: New nearest (32235): restore.py
restore.py

关键字:macOS,OSX,文件出生时间,出生时间,创建时间,stat,mtime,ctime,atime