Hazel easy file management - wechat chat files are automatically classified and stored

First, the video to achieve the effect on: https://v.qq.com/x/page/x3021yp3u3a.html

  1. Software used to realize this function
    1. Hazel: you can download it on the Internet
    2. Automator: the self-contained Automator on the Mac can search through command+space
  2. Specific functions to be realized
  • When files (Excel, Word, PDF, etc.) sent by wechat friends are received, they are automatically saved to the corresponding directory, and the file type can be set through the variable filterRules;
  • The file receives a time limit. For example, only the files in the last 5 minutes are processed. It is configured through the parameter filterTime, and the unit is minutes
  • The directory for automatically saving files can be set through the variable chooseFromList, or you can manually select other file directories
  • The file already exists. A pop-up window will prompt whether to overwrite it
  • Mark the files and tag each file (e.g. tag excel for Excel document) to facilitate future search
  • When a file is saved for the first time, it is opened for the first time by default
  1. Usage scenario
  • Work related files: I will choose to move to the work related directory. Here, I will move to the automatic processing directory. Hazel will listen to the directory, classify and label the files. If it is a log file, I will add a time prefix before the file name by default and open the log file
  • Personal files: I will select a directory to save
  1. Implementation steps
    1. The first is to create Hazel folder rules
  1. Focus on the rules of the MessageTemp folder
  • First, how to let Hazel know that a friend in wechat sent you a file. Here, you only need to listen to the folder of wechat chat file cache, which is roughly located in the directory of / Users/xxxxxx/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/09af4488ac59013c076d683513a0e252/Message/MessageTemp /;
  • Then, when listening to a new file in this folder, trigger an Automator workflow to move the file. Why can't Hazel move it directly here? Because there are many subfolders in the MessageTemp directory, and the chat file is in a subfolder, Hazel can't determine which new file needs to be moved, So here we use the Automator workflow to move;

There are many subfolders in the MessageTemp directory

  1. Implementation of Automator workflow

Step 1: create an Automator of type workflow

Step 2: implement the Automator workflow

  • Process 1: get variable filterRules filter rules
  • Process 2: get the File directory triggered by Hazel monitoring (a subdirectory under MessageTemp), then get all the files in the OpenData and File directory under the folder, and then execute the filter rules to filter out the files that do not need to be processed
  • Process 3: convert the file into a file path (now it seems a little redundant)
  • Process 4: get the value of the variable chooseFromList
  • Process 5: let the user select the target directory
  • Process 6: get the target directory selected by the user and all files to be processed, and copy the files

The AppleScript script corresponding to process 2 is as follows:

on run {input, parameters}
	-- Get filter rule
	set rule to item -1 of input
	--take input Set to pending directory
	set input to item 1 of input
	set folderPath1 to (input as text) & "OpenData:"
	set folderPath2 to (input as text) & "File:"
	tell application "Finder"
		try
			set files1 to get document files of folder folderPath1
			set files2 to get document files of folder folderPath2
			set allFiles to files2
			--Filtered file
			set filterFiles to {}
			set theList to {}
			repeat with aFile in allFiles
				--File extension
				set extensionName to the name extension of aFile
				--File name
				set fileName to the name of aFile
				try
					set SuffixRules to suffix of rule
					set stopStartRule to false
					--Creation time
					set theFileDate to (creation date of aFile as string)
					set oneminutes to (current date) - 1 * minutes
					if theFileDate > oneminutes then
						--Whether it is in the extension rule. If it is, it is directly put in filterFiles No subsequent rule filtering
						repeat with suffix in SuffixRules
							
							if (extensionName as text) ends with suffix then
								set the end of filterFiles to aFile
								set stopStartRule to true
								exit repeat
							end if
						end repeat
						-- Rules that start with what and don't end with what are and
						if stopStartRule is false then
							-- What to start with and what not to end with
							set startRules to start of rule
							repeat with startRule in startRules
								if fileName starts with startRule then
									set notEndRules to notEnd of rule
									set needAdd to true
									repeat with notEndRule in notEndRules
										if extensionName = notEndRule then
											needAdd without to
											exit repeat
										end if
									end repeat
									if needAdd is true then
										set the end of filterFiles to aFile
										exit repeat
									end if
								end if
							end repeat
						end if
					end if
				on error the error_message number the error_number
					display dialog "Error: " & the error_number & "." & the error_message buttons {"OK"}
				end try
			end repeat
			return filterFiles
		on error the error_message number the error_number
			display dialog "Error: " & the error_number & "." & the error_message buttons {"OK"}
		end try
	end tell
end run

The AppleScript script corresponding to process 3 is as follows:

on run {input, parameters}
	if (count of input) ≤ 0 then
		-- There are no files to move
		display notification "There are no files to move"
		return {}
	else
		local params
		set params to {}
		repeat with aFile in input
			set curPath to aFile as text
			set params to params & curPath & "|"
		end repeat
		return params
	end if
end run

on theSplit(theString, theDelimiter)
	-- Save the separator to restore the old settings
	set oldDelimiters to AppleScript's text item delimiters
	-- Set separator usage
	set AppleScript's text item delimiters to theDelimiter
	-- Create an array and the array theArray Is a text type
	set theArray to text items of theString
	--Restore old settings
	set AppleScript's text item delimiters to oldDelimiters
	return theArray
end theSplit

The AppleScript script corresponding to process 5 is as follows:

on run {input, parameters}
	-- Get drop-down box option value
	set chooseFromList to item -1 of input
	set chooseFromList to theSplit(chooseFromList, "|")
	--Eliminate variable parameters
	set input to items 1 through -2 of input
	if input is {} then
		return {}
	else
		set flodPath to choose from list chooseFromList & "Select Folder"
		--display dialog ((flodPath) as text)
		if flodPath is false then
			return
		end if
		set re to item -1 of flodPath
		if re = "Select Folder" then
			set folderSelected to choose folder "Select a folder"
			if folderSelected is false then return
			set re to POSIX path of folderSelected
		end if
		set input to input & re
		return input
	end if
	(* Your script goes here *)
	
	return input
end run


on theSplit(theString, theDelimiter)
	-- Save the separator to restore the old settings
	set oldDelimiters to AppleScript's text item delimiters
	-- Set separator usage
	set AppleScript's text item delimiters to theDelimiter
	
	-- Create an array and the array theArray Is a text type
	set theArray to text items of theString
	
	--Restore old settings
	set AppleScript's text item delimiters to oldDelimiters
	
	return theArray
end theSplit

The AppleScript script corresponding to process 6 is as follows:

on run {input, parameters}
	if the length of input < 2 then
		return
	end if
	--Get the target road strength
	set targetPath to item -1 of input
	--Get file strength
	set filesPaths to items 1 through -2 of input
	repeat with aFile in filesPaths
		try
			
			set theLength to the length of ((aFile) as text)
			if (theLength > 0 and isDirectory(aFile) is false) then
				
				set filePath to POSIX path of aFile as text
				tell application "Finder" to set fileExists to exists my POSIX file filePath
				if fileExists is true then
					set fArray to my theSplit((filePath) as string, "/")
					set fname to item -1 of fArray
					--Judge whether there is a file with the same name in the target road
					set targetPathFile to targetPath & fname
					set file1 to POSIX file filePath
					set relTargetPath to POSIX file targetPath as text
					tell application "Finder" to set targetPathFileExists to exists my POSIX file targetPathFile
					
					if targetPathFileExists is true then
						set tempVar to display dialog "file [" & fname & "] Already exists,Overwrite" buttons {"yes", "no"}
						set theButtonPressed to button returned of tempVar
						if theButtonPressed = "yes" then
							tell application "Finder"
								--Overwrite replication
								duplicate file1 to relTargetPath with replacing
								display notification "Successfully moved to:" & targetPath with title ("file: " & fname as text)
							end tell
						else
							--exit repeat
						end if
					else
						tell application "Finder"
							--copy
							duplicate file1 to relTargetPath
							display notification "Successfully moved to:" & targetPath with title ("file: " & fname as text)
						end tell
					end if
					
					
				end if
			end if
		on error the error_message number the error_number
			display dialog "Error: " & the error_number & "." & the error_message buttons {"OK"}
		end try
	end repeat
end run

on theSplit(theString, theDelimiter)
	--display dialog ((theString) as text)
	-- Save the separator to restore the old settings
	set oldDelimiters to AppleScript's text item delimiters
	-- Set separator usage
	set AppleScript's text item delimiters to theDelimiter
	
	-- Create an array and the array theArray Is a text type
	set theArray to text items of theString
	
	--Restore old settings
	set AppleScript's text item delimiters to oldDelimiters
	
	-- return the result
	return theArray
end theSplit

on isDirectory(someItem)
	set filePosixPath to quoted form of (POSIX path of someItem)
	set fileType to (do shell script "file -b " & filePosixPath)
	if fileType ends with "directory" then return true
	return false
end isDirectory
  1. summary

The above is all the steps to realize this function. If necessary, you can also download it in the attachment at the back of the article. The implementation process has gone through hardships because you have not been in contact with AppleScript language before.

Attachment: judge whether to move. workflow download

Automator CICD Consul Consul-template docker docker-compose elasticsearch gitlab gitlab-runner harbor Hazel hybris java kubernetes mac macx high efficiency nginx rancher spring boot spring cloud swarm Distributed serial number There are rules

Posted on Fri, 26 Nov 2021 12:35:20 -0500 by horsleyaa