Last active
November 24, 2018 04:45
-
-
Save thnk2wn/e3228ebde7ff9c7f3c948fed5dacb1a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env pwsh | |
| # ./move-ios-media.ps1 '/Users/hudgeo/Desktop/camera' | |
| # ./move-ios-media.ps1 '/Users/hudgeo/Desktop/camera', '/Users/hudgeo/Desktop/iGeoffCamera' | |
| param( [Parameter(Mandatory=$true)] [string[]] $directories ) | |
| Write-Host "Using Powershell $($PSVersionTable.PSEdition) v $($PSVersionTable.PSVersion)" | |
| $logFile = "$HOME/Scripts/move-ios-media.txt" | |
| $picsRoot = "$HOME/Pictures/By Year"; $vidRoot = "$HOME/Movies/By Year" | |
| $preview = $false; $autoOpenPathFinder = $true | |
| Start-Transcript -Path $logFile | |
| function Show-Notification ($title, $msg, $folder) { | |
| if (!$env:notifierInstalled) { | |
| "Ensuring terminal-notifier is installed with homebrew (w/o brew update)" | |
| $env:HOMEBREW_NO_AUTO_UPDATE = 1 | |
| /usr/local/bin/brew install terminal-notifier | |
| $env:notifierInstalled = $true | |
| } | |
| $subtitle = "Click to open folder" | |
| $photosImage = "https://i.imgur.com/mjW8IQd.jpg"; $finderImage = "https://i.imgur.com/Saz2BG2.png" | |
| Write-Host "Showing notification with message ""$msg"", title ""$title"", subtitle ""$subtitle""" | |
| /usr/local/bin/terminal-notifier -title "$title" -subtitle $subtitle -message "$msg" -execute "open '$folder'" -appIcon $photosImage -contentImage $finderImage -sound "Pop" | |
| } | |
| # Move-Media '/Users/hudgeo/Desktop/Camera' -Whatif | |
| function Move-Media { | |
| [CmdletBinding(SupportsShouldProcess = $true)] | |
| param($directory) | |
| Write-Host "Moving media in $directory. What if? $WhatIf" | |
| $moveToMap = @{}; $vidMovedCount = 0; $picMovedCount = 0 | |
| Get-ChildItem $directory | ` | |
| Where-Object { ".mov", ".mp4", ".m4v", ".3gp", ".jpg" -contains $_.Extension} | ` | |
| ForEach-Object { | |
| $file = $_ | |
| $isPic = ($file.Extension -eq '.jpg') | |
| $moveToPath = Join-Path @{$true="$picsRoot";$false=$vidRoot}[$isPic] $file.LastWriteTime.ToString("yyyy/yyyy-MM") | |
| if (!(Test-Path $moveToPath) -and $PSCmdlet.ShouldProcess($moveToPath, "Create Directory")) { | |
| Write-Host "Creating $moveToPath" | |
| New-Item -ItemType Directory $moveToPath -Confirm:$false -Force | Out-Null | |
| } | |
| if ($PSCmdlet.ShouldProcess($file.FullName, "Move File to $($moveToPath)")) { | |
| Write-Host "Moving $($file.FullName) to $moveToPath" | |
| Move-Item -Path $file.FullName -Destination $moveToPath -Force -Confirm:$false | |
| } | |
| $moveToMap[$moveToPath]++ | |
| if ($isPic) { $picMovedCount++ } else { $vidMovedCount++ } | |
| } | |
| foreach ($dir in $moveToMap.GetEnumerator()) { | |
| # Shorten directory name for title so it's not cut off. i.e. "Pictures/../2018-04" | |
| $shortDirName = $dir.Name.Split('/')[3,6] -join "/../" | |
| Show-Notification "$shortDirName" "$($dir.Value) new file(s) have been added." "$($dir.Name)" | |
| if ($autoOpenPathFinder) { | |
| Write-Host "Opening Finder to $($dir.Name)" | |
| Invoke-Item $dir.Name | |
| #open -a "Path Finder.app" $dir.Name # Window/tab gets reused, not sure on new tab yet | |
| } | |
| } | |
| Write-Host "Deleting $directory/*.AAE" | |
| # -WhatIf:$preview not behaving as expected. Don't want to wrap in ShouldProcess, want per file what if. | |
| if (!$WhatIf) { Remove-Item "$directory/*.AAE" } else { Remove-Item "$directory/*.AAE" -WhatIf } | |
| Show-Notification $directory "Moved $picMovedCount pictures, $vidMovedCount videos." $directory | |
| } | |
| ForEach ($directory in $directories) { | |
| Move-Media $directory -WhatIf:$preview | |
| } | |
| Invoke-Item $logFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment