Skip to content

Instantly share code, notes, and snippets.

@SoraAzure
Forked from menushka/airdropSorter.scpt
Last active March 12, 2023 12:21
Show Gist options
  • Save SoraAzure/0f56a11133c4b577ad35184b99434d0a to your computer and use it in GitHub Desktop.
Save SoraAzure/0f56a11133c4b577ad35184b99434d0a to your computer and use it in GitHub Desktop.
Avoid overwriting files with the same name in the target folder by appending a number suffix.
property destinationFolder : "Path:to:AirDrop:Folder:in:Alias:format" -- Please change this
property quarantineTypes : { "59" }
on run {input, parameters}
tell application "Finder"
set filesToMove to {}
repeat with iInput from 1 to count input
set filePath to item iInput of input
set shouldProcess to false
if (count quarantineTypes) > 0 then
set quarantine to getFileQuarantineType(POSIX path of filePath)
repeat with iQuarantineType from 1 to count quarantineTypes
set quarantineType to item iQuarantineType of quarantineTypes
if quarantineType = quarantine then
set shouldProcess to true
exit repeat
end if
end repeat
else
set shouldProcess to true
end if
if shouldProcess then
set repeater to ""
set newFileName to name of filePath
if exists ((destinationFolder as string) & name of filePath) then
repeat
if repeater is not "" then
set repeaterString to " " & repeater
else
set repeaterString to ""
end if
set newFileName to my getFilePureName(POSIX path of filePath) & repeaterString & my getFileExtensionName(POSIX path of filePath, true)
if exists ((destinationFolder as string) & newFileName) then
if repeater is not "" then
set repeater to repeater + 1
else
set repeater to 2
end if
else
exit repeat
end if
end repeat
end if
set sourceFolder to (folder of filePath) as string
set name of filePath to newFileName
set newFilePath to sourceFolder & newFileName
set end of filesToMove to newFilePath
end if
end repeat
move filesToMove to destinationFolder with replacing
end tell
return input
end run
on getFileQuarantineType(filePath)
return do shell script "ls -l -@ '" & filePath & "' | tr '\\n' ' ' | sed 's/.*com\\.apple\\.quarantine\\s*\\(\\d*\\)/ \\1/' | awk '{$1=$1};1'"
end getFileQuarantineType
on getFilePureName(filePath)
tell application "System Events"
tell disk item filePath to set {pureName, extension} to {name, name extension}
if extension is not "" then
set pureName to text 1 thru -((count extension) + 2) of pureName
end if
end tell
return pureName
end getFilePureName
on getFileExtensionName(filePath, withDot)
tell application "System Events"
tell disk item filePath to set extension to name extension
end tell
if extension is not "" and withDot then
set extension to "." & extension
end if
return extension
end getFileExtensionName
@ianrobertdouglas
Copy link

Does this still work for you? I get applescript error 1721 running this. I'm not entirely sure what Path:to:AirDrop:Folder:in:Alias:format means. In that value, I just added the following: "/Users/MyUserName/Airdrop" Maybe that's the problem?

@Peter-Schorn
Copy link

Peter-Schorn commented Nov 28, 2019

Does this still work for you? I get applescript error 1721 running this. I'm not entirely sure what Path:to:AirDrop:Folder:in:Alias:format means. In that value, I just added the following: "/Users/MyUserName/Airdrop" Maybe that's the problem?

For every / in the file path, replace it with : This is what the alias format is. Therefore, your filepath would be Users:MyUserName:Airdrop

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