Я думаю, было бы ужасно пытаться сделать это в яблочный скрипт , поэтому я сделал это в bash
, который в любом случае установлен на всех компьютерах Mac.
Вы также можете вызвать мой скриптиз яблочный скрипт в любом случае, используя do shell script
.
Таким образом, вы бы сохранили следующее в вашем домашнем каталоге как 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
Затем вы можете запустить скрипт так, чтобы найти ближайший файл с рождением до 09:00:00 в каталоге /Users/mark/StackOverflow
:
$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