Check if file names are the same using Applescript - applescript

How would I go about checking if a file (an alias called theFile) has the same name as any other files in another folder?
I would like to have an Applescript look through all of the files in the folder and has a if and else as a result if it does match one of the names or doesn't.

Choose a folder
Get the name of theFile
Get a list of all file names in the folder
Check if the list contains the name of theFile
set theFile to alias ...
set theFolder to choose folder
tell application "System Events"
set theFileName to name of theFile
set otherFileNames to name of files of theFolder
end tell
set fileNameExists to otherFileNames contains theFileName

Related

Apple Script: Copy file (being selected from prompt) to another location

I am struggling with my Apple Script. I am asking to select a file and the selected file needs to be copied to another location. I am new to Apple Scripting, so probably made some mistake. I tried different versions with "copy" instead of "duplicate" or "alias" instead of "file", but nothing worked so far. Hope, somebody can help me figure this out.
This is what I scripted so far (I get an AppleEvent timed out):
set DefaultPath to POSIX file "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Test"
set DestFolder to "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Destination"
set theFile to (choose file with prompt "Select file:" default location (DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell
The problem is that the Finder doesn't support POSIX paths.
I recommend to use a relative path path to library folder from user domain and HFS paths (colon separated)
To satisfy the location parameter of choose file put the alias keyword in front of the (HFS) path
set cloudDocs to (path to library folder from user domain as text) & "Mobile Documents:com~apple~CloudDocs:"
set DefaultPath to cloudDocs & "FOLDER:Test:"
set DestFolder to cloudDocs & "FOLDER:Destination:"
set theFile to (choose file with prompt "Select file:" default location (alias DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell

AppleScript - Copy All files/folders from source folder to destination folder

I'm trying to write an AppleScript that will simply copy the contents (both folders and files) from a specified source folder to a specified destination folder. At the moment my script runs but only copies one file and I can't work out how to get it to copy all files in the folder.
Here's my script:
set sourceFolder to (POSIX file "/Users/benny/Desktop/Projects/Source/Project1") as alias
set destinationFolder to (POSIX file "/Users/benny/Documents/Masters/Project1") as alias
tell application "System Events"
set availableSourceFiles to every file of sourceFolder whose visible is true
set filesOfTargetFolder to files of destinationFolder whose visible is true
end tell
-- if no more source file is available, quit this script
if (count of availableSourceFiles) = 0 then
quit
end if
set sourceFile to (first item of availableSourceFiles) as alias
-- use the Finder to copy the file
tell application "Finder"
-- duplicate the file to the target folder
duplicate sourceFile to destinationFolder
end tell
I'm assuming I need to include a for each type loop but can't get the syntax correct here. Haven't written AppleScripts in many years so trying to remember how it all works.
If the destination "Project1" folder doesn't have stuff in it already, then duplicating the folder is likely to be quicker:
tell application id "com.apple.Finder" to duplicate folder POSIX file ¬
"/Users/benny/Desktop/Projects/Source/Project1" to the folder ¬
POSIX file "/Users/benny/Documents/Masters" with replacing
However, if that's not an option, then I'd stick with your method and copy the contents of the folder across instead:
set here to POSIX file "/Users/benny/Desktop/Projects/Source/Project1"
set there to POSIX file "/Users/benny/Documents/Masters"
tell application id "com.apple.Finder" to duplicate ¬
every item in the folder here to there
Bear in mind that if there's a file or folder at any of the destinations that intended to be occupied by one of the incoming source items, Finder will throw an error. You would typically incorporate some sort of check ahead of the copy.

Applescript Error - Can't Get every file of ... folder?

I have a script that someone else wrote. It works on his computer but not mine.
Originally it said "as alias", but I kept getting an error message as follows:
"Can’t make file "Macintosh HD:Users:williamsato:Desktop:Photobooth:CurrentSession" into type alias."
So I changed alias to string, following a suggestion made in another post.
Now I don't get that error message, but I do get a new one as shown below:
"Can’t get every file of "Macintosh HD:Users:williamsato:Desktop:PhotoBooth:CurrentSession"."
And it highlights the line "move files of SourceFolder to DestinationFolder with replacing"
Not sure what is going wrong.
on run {input, parameters}
set SourceFolder to POSIX file "/Users:/williamsato/Desktop/Photobooth/CurrentSession" as string
set DestinationFolder to POSIX file "/Users/williamsato/Desktop/Photobooth/PreviousSessions" as string
tell application "Finder"
move files of SourceFolder to DestinationFolder with replacing
end tell
(* Clear Large Type *)
tell application "System Events" to keystroke "a" using command down
end run
Here are three different way of forming the SourceFolder and DestinationFolder variables and moving the files:
set SourceFolder to (path to desktop folder as string) & "Photobooth:CurrentSession:" as alias
set DestinationFolder to (path to desktop folder as string) & "Photobooth:PreviousSessions:" as alias
tell application "Finder" to move files of SourceFolder to DestinationFolder with replacing
set SourceFolder to alias "Macintosh HD:Users:williamsato:Desktop:Photobooth:CurrentSession:"
set DestinationFolder to alias "Macintosh HD:Users:williamsato:Desktop:Photobooth:PreviousSessions:"
tell application "Finder" to move files of SourceFolder to DestinationFolder with replacing
set SourceFolder to POSIX file "/Users/williamsato/Desktop/Photobooth/CurrentSession" as alias
set DestinationFolder to POSIX file "/Users/williamsato/Desktop/Photobooth/PreviousSessions" as alias
tell application "Finder" to move files of SourceFolder to DestinationFolder with replacing
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.

Import contents of specific folder into iTunes using Applescript

I have created an Applescript that will, with one click, add the contents of a specific folder on my hard drive to iTunes (I really want to avoid using the iTunes organisational system).
Applescript
tell application "iTunes"
set theFolder to ("Macintosh HD:Temp to be Listened to:Temp on iPod:")
set with_subfolders to true --list subfolders or not (recursion)
set parent_folder to theFolder
end tell
tell application "System Events"
set item_list to parent_folder
end tell
tell application "iTunes"
add contents of parent_folder to user playlist "Temp on iPod"
end tell
However, it only imports into iTunes the files from the top level/parent folder.
I want to include files from the folders in the parent folder.
Is there a way I can get it to be recursive?
You do not need to get all files from a folder, since iTunes does this automatically and recursively from a folder.
Just add the parent folder, like this:
set parent_folder to "Macintosh HD:Temp to be Listened to:Temp on iPod:" as alias
tell application "iTunes"
add parent_folder to user playlist "Temp on iPod"
end tell
Solution #1: Using entire contents of the Finder - not very fast
set theFolder to "Macintosh HD:Temp to be Listened to:Temp on iPod:"
tell application "Finder" to set filesToAdd to files of entire contents of folder theFolder as alias list
tell application "iTunes" to add filesToAdd to user playlist "Temp on iPod"
Solution #2 : Using Spotlight - very fast, but the volume must be indexed.
set theFolder to "/Temp to be Listened to/Temp on iPod"
set fileList to paragraphs of (do shell script "mdfind -onlyin " & quoted form of theFolder & space & quoted form of "kMDItemContentTypeTree == '*public.audio*'")
set filesToAdd to {}
repeat with aFile in fileList
set end of filesToAdd to POSIX file aFile as alias
end repeat
tell application "iTunes" to add filesToAdd to user playlist "Temp on iPod"

AppleScript: match and delete files in folder

My photo camera allows to save pictures in RAW and JPG in parallel. I find this convenient because on my Mac I can quickly browse the JPGs and delete the "bad" ones. Besides, I keep the RAW files of the "good" JPGs in case I need to do some deep editing.
I would like to write an AppleScript which deletes all the "bad" RAWs (RAW files which don't have a corresponding JPG anymore). All files are in the same directory.
This is my outline (far away from correct syntax!):
tell application "Finder"
set source_folder to choose folder with prompt "Please select directory."
my clearFiles(source_folder)
end tell
on clearFiles(source_folder)
set theItems to ""
tell application "System Events"
set theItems to get the name of every disk item of source_folder
end tell
repeat with theFile in theItems
if the extension of theFile is "raw" and exists name of theFile & ".jpg" then
tell finder
delete (name of theFile & ".raw") in source_folder
and tell
end if
end tell
end clearFiles
try this
set source_folder to choose folder with prompt "Please select directory."
tell application "Finder"
set rawFiles to every file of source_folder whose name extension is "raw"
repeat with aFile in rawFiles
set baseName to text 1 thru -5 of (get name of aFile)
set jpgFile to baseName & ".jpg"
if not (exists file jpgFile of source_folder) then delete aFile
end repeat
end tell

Resources