From e1e4205566dbd415309fe966942acabde4d187d1 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Tue, 22 Jul 2025 14:34:28 +0200 Subject: [PATCH] add keywords with exiftool --- src/scanbasic.sh | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/scantopdf | 9 ++++---- src/scantopdfbw | 9 ++++---- src/scantopdfgray | 9 ++++---- tests/scanbasic.bats | 22 ++++++++++++++++++++ 5 files changed, 86 insertions(+), 12 deletions(-) diff --git a/src/scanbasic.sh b/src/scanbasic.sh index c5096f5..96033d5 100644 --- a/src/scanbasic.sh +++ b/src/scanbasic.sh @@ -198,10 +198,56 @@ createmultipdfs() done } +get_title_from_file_name() +{ + local file_name="$1" + local title="${file_name%.*}" + title="${title#*_}" + title="${title//_/ }" + + echo "$title" +} + +get_date_from_file_name() +{ + local file_name="$1" + local date="${file_name%%_*}" + date="${date//-/:} 00:00:00Z" + + echo "$date" +} + +add_keywords() +{ + local file_name="$1" + local keywords="$2" + + [ -z "$keywords" ] && return 0 + + [[ "$file_name" =~ [0-9]{4}\-[0-9]{2}\-[0-9]{2}_.*\.pdf ]] \ + || errlog "file name mismatch pattern" \ + || return $? + + local title=$(get_title_from_file_name "$file_name") + local date=$(get_date_from_file_name "$file_name") + + echo "ℹ️ INFO: adding keywords $keywords to $file_name" + exiftool -Title="$title" \ + -CreateDate="$date" \ + -sep "," \ + -PDF:Keywords="$keywords" \ + -overwrite_original_in_place \ + "$file_name" \ + > /dev/null \ + || errlog "could not add keywords $keywords to $file_name" \ + || return $? +} + scantopdf() { local mode="$1" local pagecount="$2" local output="$3" + local keywords="$4" check_commands \ || return $? @@ -235,4 +281,7 @@ scantopdf() { createonepdf "$output" \ || return $? + + add_keywords "$output" "$keywords" \ + || return $? } diff --git a/src/scantopdf b/src/scantopdf index 4412985..f748388 100755 --- a/src/scantopdf +++ b/src/scantopdf @@ -4,15 +4,16 @@ main() { - local pagecount=$1 - local output=$2 + local pagecount="$1" + local output="$2" + local keywords="$3" echo "⭐ START: scantopdf" - scantopdf "Color" "$pagecount" "$output" \ + scantopdf "Color" "$pagecount" "$output" "$keywords"\ || return $? echo "✅ SUCCESS: scantopdf" } -main $* +main "$@" diff --git a/src/scantopdfbw b/src/scantopdfbw index b1ff268..e37b289 100755 --- a/src/scantopdfbw +++ b/src/scantopdfbw @@ -4,15 +4,16 @@ main() { - local pagecount=$1 - local output=$2 + local pagecount="$1" + local output="$2" + local keywords="$3" echo "⭐ START: scantopdfbw" - scantopdf "BW" "$pagecount" "$output" \ + scantopdf "BW" "$pagecount" "$output" "$keywords" \ || return $? echo "✅ SUCCESS: scantopdfbw" } -main $* +main "$@" diff --git a/src/scantopdfgray b/src/scantopdfgray index 103a175..2f2f7cf 100755 --- a/src/scantopdfgray +++ b/src/scantopdfgray @@ -4,15 +4,16 @@ main() { - local pagecount=$1 - local output=$2 + local pagecount="$1" + local output="$2" + local keywords="$3" echo "⭐ START: scantopdfgray" - scantopdf "Gray" "$pagecount" "$output" \ + scantopdf "Gray" "$pagecount" "$output" "$keywords" \ || return $? echo "✅ SUCCESS: scantopdfgray" } -main $* +main "$@" diff --git a/tests/scanbasic.bats b/tests/scanbasic.bats index 069db78..8fefc46 100755 --- a/tests/scanbasic.bats +++ b/tests/scanbasic.bats @@ -242,6 +242,18 @@ setup() { assert_success } +@test "should get title from filename 2025-07-22_test_12345_word.pdf" { + run get_title_from_file_name "2025-07-22_test_12345_word.pdf" + + assert_line --index 0 "test 12345 word" +} + +@test "should get date from filename 2025-07-22_test_12345_word.pdf" { + run get_date_from_file_name "2025-07-22_test_12345_word.pdf" + + assert_line --index 0 "2025:07:22 00:00:00Z" +} + @test "should failure scantopdf missing mode parameter" { run scantopdf @@ -312,3 +324,13 @@ setup() { assert_success } + +@test "should success add keywords" { + exiftool() { + : + } + + run add_keywords "2025-07-22_test_word.pdf" + + assert_success +} -- 2.47.3