I made a post a while back (around a week ago) asking for some clarification on some scripts I've been writing. I'm a new System Admin (5 months into the job) and I got a project that is baffling me. Once again, I'm not sure if its my inexperience or if I'm over thinking it, but this is what I have so far!
(I’m not trying to get out of doing my work, I’m more or less looking for guidance!)
Part #1: Works fine!
set desktopFolder to path to desktop as text
set thefile to desktopFolder & "lol.html"
set DestinationPath to desktopFolder & "New Project 4 Neil:"
set destinationFilename to "ROFL2.txt"
with timeout of 60 seconds
tell application "Finder"
try
set theDupe to duplicate file thefile to folder DestinationPath
set name of theDupe to destinationFilename
on error
display dialog "Oops, a problem occurred duplicating the file. This may be because the file already exists!"
end try
end tell
end timeout
Part #2: First I am attempting to list all the file names in a selected directory; then I am attempting to classify what files the script will be modifying. One of many things I am having trouble with is the instructions to have the script change a .html to “xxx”; am I being naive or do I actually need to know functions need to be achieved to continue on?
Also how do I dictate to the script what actually loops? Will AppleScript be aware I am targeting or the previous list?
tell application "Finder"
set file_list to name of every file of entire contents of (choose folder with prompt "Please select directory.")
end tell
set jpegImage to {"jpeg", "jpg"}
set htmlfile to {"html"}
tell application "Finder" ---Select by extension
(files of folder "hd:users:mactech:desktop:" where name extension is in jpegImage) as alias list
(files of folder "hd:users:mactech:desktop:" where name extension is in htmlfile) as alias list
end tell
I’m going to use pseudo scripting to get my idea of what “I” think I need to do:
tell application "Finder"
set file_list to name of every file of entire contents of (choose folder with prompt "Please select directory.")
end tell
set jpegImage to {"jpeg", "jpg"}
set htmlfile to {"html"}
tell application "Finder"
(files of folder "hd:users:mactech:desktop:" where name extension is in jpegImage) as alias list
loop
if file in list is jpg rename to "123.jph"
end loop
(files of folder "hd:users:mactech:desktop:" where name extension is in htmlfile) as alias list
end tell
loop
if file in list is html rename to "123.html"
end loop
Go read this
And visit the Unscripted section of this website.
Re script #2: if you want to do things to the collected files you’d put that list into a variable:
tell application "Finder" -- Select by extension
set imageFiles to (files of folder "hd:users:mactech:desktop:" where name extension is in jpegImage) as alias list
set htmlFiles to (files of folder "hd:users:mactech:desktop:" where name extension is in htmlfile) as alias list
end tell
Re script #3: I would avoid changing extensions. AppleScript has no other reliable way of telling what a file is (AFAIK).
And the other thing: to do things to a list of things you use a repeat construct - AppleScript has about half a dozen varieties.
repeat with anImageFile in imageFiles
-- clever stuff happens here
end repeat
For me, the best way to change filenames is thru using AppleScript text item delimiters. I believe there’s at least one article in the Unscripted section. Googling this site will turn up heaps of links.
The exact recipe is very much dependent on your requirements.
Hi. I agree that you probably should do some additional introductory reading, however, a file could be a JPEG and have no name extension, by virtue of its kind property. There could also be multiple files in the same folder that are JPEGs, and they can’t all be identically renamed; I would probably choose to serialize them.
set counter to 123
set p2d to path to desktop --alias
tell application "Finder"
repeat with aFile in (p2d's files whose its name ends with ".jpeg" or name ends with ".jpg" or kind is "JPEG image") as alias list
set aFile's name to (counter as text) & " whatever" & ".jpg" --concatenate with optional suffixes
set counter to counter + 1
end repeat
--set my counter to 123 --reset, if desired
repeat with aFile in (p2d's files whose its name ends with ".html") as alias list
set aFile's name to (counter as text) & ".html"
set counter to counter + 1
end repeat
end tell
Mmm. I tested that - removed extension from image. Both its kind and its kMDitemKind changed to “Document”. That’s why I said “no reliable way”.
It still opens in Preview, and Preview knows what it is. How?
The Spotlight Database, is a separate entity to the file system, it isn’t used for opening anything, it is just a meta database, giving metadata about our files. The files names, their attributes and extended attributes is metadata that resides with the file system, either in the inodes, or resource forks, the jpg file in question probably had a Creator Code, File Type, or UTI that was set to something that Preview reckognizes, as a jpeg file, which is probably the attributes that Preview reckognizes, after it has tried to reckognize the file type by its suffix and failed.
Edit
If you open the file from Finder, and it turns up in Preview, then it is of course Finder, that first decides where to send it, based on the attributes the file posses, it probably finds a matching app for the particular file by LaunchServices, or just delegates the whole task to LaunchServices, (passes the alias of the file, and lets LaunchServices take it from there).
tell application "Finder"
set file_list to name of every file of entire contents of (choose folder with prompt "Please select directory.") --choosing which directory
end tell
set jpegImage to {"jpg", "jpeg", ".jpg"} --not sure if I need the "." in .jpg for file extention
set htmlFiles to {"html", ".html"}
tell application "Finder"
set imageFiles to (files of folder "hd:users:mactech:desktop:test folder" where name extension is in jpegImage) as alias list
set htmlFiles to (files of folder "hd:users:mactech:desktop:test folder" where name extension is in htmlFiles) as alias list
end tell
set counter to 123
set p2d to path to desktop --alias to desktop
tell application "Finder"
repeat with aFile in (p2d's files whose its name ends with ".jpeg" or name ends with ".jpg" or kind is "JPEG image") as alias list
set aFile's name to (counter as text) & ".jpg" --concatenate with optional suffixes
set counter to counter + 1
end repeat
--set my counter to 123 --reset, if desired
repeat with aFile in (p2d's files whose its name ends with ".html") as alias list
set aFile's name to (counter as text) & ".html"
set counter to counter + 1
end repeat
end tell
When I attempt to execute the script I get this error
Can’t make every file of «class cfol» “hd:users:mactech:desktop:test folder” of application “Finder” whose {“jpg”, “jpeg”, “.jpg”} contains name extension into type alias list.