Skip to content

Instantly share code, notes, and snippets.

@selfboot
Created October 30, 2023 01:43
Show Gist options
  • Save selfboot/52d5746e238db8a5b294647abba19a45 to your computer and use it in GitHub Desktop.
Save selfboot/52d5746e238db8a5b294647abba19a45 to your computer and use it in GitHub Desktop.
mac automator tool,Monitor folder, automatically compress and upload images to COS.
on run {input, parameters}
set currentDate to do shell script "date '+%Y%m%d'"
tell application "System Events"
repeat with i in input
set filePath to POSIX path of i
set fileExtension to name extension of i
set folderName to do shell script "dirname " & filePath
set fileName to name of i
set AppleScript's text item delimiters to "."
set baseName to first text item of fileName
set regexMatch to do shell script "if [[ " & baseName & " =~ ^[0-9]{8}_ ]]; then echo 'true'; else echo 'false'; fi"
if fileExtension is "png" and regexMatch is "false" then
do shell script "/opt/homebrew/bin/pngquant --force --ext .png " & quoted form of filePath
set newBaseName to currentDate & "_" & baseName
set newFileName to newBaseName & "." & fileExtension
set name of i to newFileName
set newFilePath to folderName & "/" & newFileName
else if fileExtension is in {"webp", "jpeg", "gif"} and regexMatch is "false" then
set newBaseName to currentDate & "_" & baseName
set newFileName to newBaseName & "." & fileExtension
set name of i to newFileName
set newFilePath to folderName & "/" & newFileName
else if regexMatch is "true" then
set newFilePath to filePath
end if
set uploadCommand to "/opt/homebrew/Caskroom/miniconda/base/bin/coscmd upload " & quoted form of newFilePath & " /"
do shell script uploadCommand
end repeat
end tell
end run
@selfboot
Copy link
Author

Note that the coscmd path(/opt/homebrew/Caskroom/miniconda/base/bin/coscmd) is changed to your own.

@selfboot
Copy link
Author

selfboot commented Apr 20, 2025

Support conversion to webp format to reduce file size

brew install imagemagick

on run {input, parameters}
	set currentDate to do shell script "date '+%Y%m%d'"
	tell application "System Events"
		repeat with i in input
			set filePath to POSIX path of i
			set fileExtension to name extension of i
			set folderName to do shell script "dirname " & filePath
			set fileName to name of i
			set AppleScript's text item delimiters to "."
			set baseName to first text item of fileName
			set regexMatch to do shell script "if [[ " & baseName & " =~ ^[0-9]{8}_ ]]; then echo 'true'; else echo 'false'; fi"
			if fileExtension is "png" and regexMatch is "false" then
				do shell script "/opt/homebrew/bin/pngquant --force --ext .png " & quoted form of filePath
				do shell script "/opt/homebrew/bin/convert " & quoted form of filePath & " " & quoted form of (do shell script "echo " & quoted form of filePath & " | sed 's/\\.png$/.webp/'")
				set newBaseName to currentDate & "_" & baseName
				set newFileName to newBaseName & "." & fileExtension
				set name of i to newFileName
				set newFilePath to folderName & "/" & newFileName
			else if fileExtension is in {"webp", "jpeg", "gif"} and regexMatch is "false" then
				set newBaseName to currentDate & "_" & baseName
				set newFileName to newBaseName & "." & fileExtension
				set name of i to newFileName
				set newFilePath to folderName & "/" & newFileName
			else if regexMatch is "true" then
				set newFilePath to filePath
			end if
			set uploadCommand to "/opt/homebrew/Caskroom/miniconda/base/bin/coscmd upload " & quoted form of newFilePath & " /"
			do shell script uploadCommand
		end repeat
	end tell
end run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment