I am new to applescript and am tying to automate a project i am working on. I will not load the code because it is 4 miles long (there is probably a more efficient way) but the general sense is something along the lines of:
if directory ~/Library/Application' 'Support/kaiotemp exists then
do code 1
else
do code 2
end
I have tried my hardest but cant even come up with a base model that doesn't crash. Any ideas?
The application Finder knows how to determine if a file or folder exists. Try this...
set folderPath to (path to home folder as text) & "Library:Application Support:kaiotemp:"
set folderExists to false
tell application "Finder"
if folder folderPath exists then set folderExists to true
end tell
if folderExists then
--do code 1
else
--do code 2
end if
Notice my path versus your path. Applescript uses colon ":" delimited paths and the path starts with the name of your hard drive (I used the "path to" command to find the path to your home folder directly in this case). So you will need to take that into account if you have other paths in your code. You'll have to study how to convert posix paths to applescript paths.
Related
I want to look into a folder to select every file whose name contains "abc".
Here is my AppleScript code:
set myFolder to (((path to library folder from user domain) as string) & "FOLDER") as alias
tell application "Finder"
set deleted123 to every file of folder myFolder whose name contains "abc"
repeat with oneFile in deleted123
if exists (deleted123) then set end of deleted123 to oneFile & return
--Do something
end repeat
if deleted123 ≠ {} then
--Do something else with the selected.
end if
end tell
The code works flawlessly on High Sierra, i.e., it finds out all the files whose names contain "abc", but it doesn't on Monterey.
What is the problem? How can this piece of code be improved?
Help highly appreciated.
There are two major mistakes:
myFolder is an alias specifier. You add the folder keyword which is a double reference, delete as alias in the first line
oneFile is a Finder file specifier, you cannot append the string return. Maybe the file specifier is silently coerced to string in Sierra. If you want to use the file references then the return statement makes no sense anyway.
Another bad practice is to modify the array deleted123 while being enumerated. Create an extra variable.
And the repeat loop makes no sense either because exists (deleted123) is always true and even if you check the current item in the loop oneFile it's always true.
When trying to duplicate files to another folder, the script throws a ~10006 error. This only occurs on some Mac mini computers and works fine on others. I have no idea why it is working on some computers but not on others.
This is the error shown:
Can't set "Macintosh HD:Users:username:Documents:" to <> "Macintosh HD:Users:username:Downloads:new test:portal resources" of application "Finder". (~10006)
tell application "Finder"
set folderToBeMoved to (container of (path to me) as text) &
"portal_resources"
set destinationFolder to path to documents folder as text
set moveFolder to duplicate folder folderToBeMoved to destinationFolder with replacing
end tell
expected output is duplicating file to documents folder. But, when testing on certain Macs, the script shows an error ~10006. It works on other macs perfectly well.
You are going to copy the folder to a literal string (path) which can fail on machines running older system versions.
Remove the as text parameter to get an alias specifier
set destinationFolder to path to documents folder
Try this code:
tell application "Finder"
set folderToBeMoved to folder "portal_resources" of container of (path to me)
set destinationFolder to path to documents folder
set moveFolder to duplicate folderToBeMoved to destinationFolder with replacing
end tell
There were two issues here that I've changed. First, you convert things to text strings and try to modify the strings, but the Finder has a rich language for talking about file objects. You should just leave everything in object form. For instance, this:
folder "portal_resources" of container of (path to me)
tells the finder to find the folder of that name in that container and return an object specifier that you can use directly.
Second, once you have this object, you can't add the 'folder' specifier to it. Where you say:
duplicate folder folderToBeMoved
folderToBeMoved is already an object specifier (an object of the form 'folder [path]') so you're actually asking the Finder for 'folder folder [path],' which throws the error you're seeing. It's like saying to someone "pass the 'pass the salt.'" People are probably smart enough to figure that out; the Finder isn't.
I just stumbled across one of the most awkward behaviours of a programming language in my live.
Guess what this AppleScript is doing:
set workspace to "tmp"
set folder1 to "08_0000012_11"
set folder2 to "8_12_11"
tell application "Finder"
if (not (exists folder folder1 of (workspace as alias))) then
make new folder at workspace as alias with properties {name:folder1}
end if
if (not (exists folder folder2 of (workspace as alias))) then
make new folder at workspace as alias with properties {name:folder2}
else
log "Folder already exists " & folder2
end if
end tell
It should create create two folders 08_0000012_11 and 8_12_11 inside the /tmp, right? ..... Wrong! It creates first one and claims the other one already exists. But it does not!
It seems that it tries to apply some logic to these names. Splits them into 3 numbers and ignores zeroes. Please tell me there is some reasonable explanation to this. Or this is happening only to me...
That's a bug in Finder. It should only ignore leading zeros when sorting file names, not when comparing them. File a bug report.
The same logic works correctly in System Events, e.g.:
set fname to "001"
tell application "System Events"
if not exists folder fname of desktop folder then
make new folder at desktop folder with properties {name:fname}
end if
end tell
I've been searching a lot for a script like this but I can't find it. I suspect it's similar to this
but not exactly, and I'm not sure how to modify it to work for me.
I have a group of files with multiple names like this....
File Name Vyear #01 (year).ext
FileName Vyear #01 (year).ext
and so forth, the convention is always the same.
Filename (sometimes multiple words) followed by V for Version then the year in parenthesis then followed by a number then another year in parenthesis. It's complicated but it's all there for a reason. What I'm looking for is a way to automate moving all those files into subfolders based on only the first part of that name. So that a file like this...
The Mist V2000 #01 (2011).zip
Would get moved to a folder named this.
The Mist V2000
I'm constantly having to make files like this and I'd love to get them sorted into sub-folders. My problem is that I'm not sure how to select just the first of the name (an account for files that have two or three words in the title) and the volume number only to create the subfolder and then match the filenames for the move.
I hope I'm explaining this properly. If anyone could help I would appreciate it.
Cheers.
Try this. Basically we can calculate the folder name if we know where the "#" character is in the file name. Then we can get all of the text up to there - 2 characters. Then you just have to make that folder and move the file into it. Simple. Good luck.
set sourceFolder to choose folder
tell application "Finder"
set theFiles to files of sourceFolder
repeat with aFile in theFiles
set fileName to name of aFile
if fileName contains "#" then
set poundOffset to offset of "#" in fileName
set folderName to text 1 thru (poundOffset - 2) of fileName
set newFolder to (sourceFolder as text) & folderName & ":"
if not (exists folder newFolder) then
make new folder at sourceFolder with properties {name:folderName}
end if
move aFile to folder newFolder
end if
end repeat
end tell
Another newbie applescript question. I'm trying to get absolute paths to all of the folders and files inside of a folder upon opening it. I'd like to write something like
on opening folder this_folder
tell application "Finder"
set everyPath to POSIX path of every item in entire contents of this_folder
end tell
repeat with n from 1 to count of everyPath
display dialog item n of everyPath
end repeat
end opening folder
But this s***s a brick so right now I have this even uglier mess.
on opening folder this_folder
tell application "Finder"
set everyName to name of every item in entire contents of this_folder
set everyPath to {}
repeat with n from 1 to count of everyName
set end of everyPath to POSIX path of item n of everyName
end repeat
end tell
repeat with n from 1 to count of everyPath
display dialog item n of everyPath
end repeat
end opening folder
Which diplays dialogs like '/file.ext' when I'm looking for something more like 'User/username/documents/folder/file.ext' and 'User/username/documents/folder/subfolder/file2.ext'.
Judging by my tendency to miss the obvious in the past, I'm assuming there's an easy way to get full path names that I'm just oblivious to, and I would appreciate any help in sorting it out. Thanks!
You can't do it the way you're trying. Normally you can just add the folder path to a name to get the full path. However you're also getting names of files in sub folders, so that won't work because you don't know the subfolder paths. As such getting names from the Finder won't help you. A faster method than the Finder for something like this may be to use the unix program "find".
set this_folder to path to desktop
-- we have to remove the trailing / from the folder path so the
-- returned paths from the find command are correct
set posix_folder to text 1 thru -2 of (POSIX path of this_folder)
-- use find to quickly find the items in this_folder
-- also filter out invisible files and files in invisible folders
-- also filter files inside of package files like ".app" files
set theItems to paragraphs of (do shell script "find " & quoted form of posix_folder & " -name \"*.*\" ! -path \"*/.*\" ! -path \"*.app/*\" ! -path \"*.scptd/*\" ! -path \"*.rtfd/*\" ! -path \"*.xcodeproj/*\"")
And if the find command isn't appropriate then you could use the mdfind command (e.g.. spotlight through the command line) in a similar manner.