Nautilus sort and move script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
# Nautilus script to move selected media files from phone, camera, etc to correct location on shared location of server

# Destination to move media to
DEST="$HOME/Share"

# Check if 'Share' folder is mounted
if [[ -z $(ls $DEST) ]]
then
  zenity --error --text="Share folder does not appear to be mounted!\n\nGet junior to fix it."
  exit 1
fi

# Function to handle jpegs
function handle_jpeg {
	local DATE_TIME=`identify -ping -format "%[EXIF:DateTimeOriginal]" $1 2>/dev/null`
	# If returned value
	if [[ -n $DATE_TIME ]]
	then
		local DATE=`echo $DATE_TIME | cut -f1 -d\ `
    echo "Photos $(echo $DATE | tr ':' ' ')"
	fi
}

# Function to handle mp4s
function handle_mp4 {
  local CREATION_TIME=`ffprobe -v quiet $1 -print_format csv -show_entries format_tags=creation_time | cut -f2 -d,`
  if [[ -n $CREATION_TIME ]]
  then
    local DATE=`echo $CREATION_TIME | cut -f1 -dT`
    echo "HomeVideos $(echo $DATE | tr '-' ' ')"
  fi
}

# mkdestdir makes a directory for a media item according to its type, year, month and day
function mkdestdir {
	local DIR="$DEST/$1/$2/$3/$4"
	mkdir -p $DIR
	echo $DIR
}

echo 'Will now process each selected file'
FILES=(${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS//;/ })
FILES_LEN=${#FILES[@]}
MOVED=0
{
  for (( i=0;i<FILES_LEN;i++ ))
  do
    file=${FILES[${i}]}
    mime=`file -b --mime-type $file`
    echo $[ $i*100/$FILES_LEN ]
    echo "#Moving $file ($[ $i+1 ]/$FILES_LEN)"
    # Check for supported mimes
    # Each supported one should set a function that 'returns' the correct typ, year, month and day
    case "$mime" in
      inode/directory)
        echo "#$file is a directory"
        continue;;
      image/jpeg) func="handle_jpeg";;
      video/mp4) func="handle_mp4";;
    esac

    # Call the function
    read typ year month day < <( $func $file )

    if [[ -n $typ && -n $year && -n $month && -n $day ]]
    then
      # Move picture using update mode
      mv -u $file $(mkdestdir $typ $year $month $day)
      ((MOVED++))
    fi
  done

  MES="Done sorting and moving media"
  if [[ $MOVED -lt $FILES_LEN ]]
  then
    MES="$MES.\n$[ $FILES_LEN-$MOVED ] items where not recognized."
  fi

  zenity --notification --window-icon="info" --text="$MES"
} |
zenity --progress --title="Sorting and moving media" --percentage=0 --auto-close