Hiding all files in a user chosen directory - applescript

So basically I want to be able to take all of the files in a folder and hide them but it always stops at this point with Applescript error -1700
tell application "Finder"
set filePath to entire contents of (choose folder with
prompt "Please choose your folder") as alias list
set pFilePath to POSIX path of filePath
end tell
Can you guys help me?

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 Image Events cant get class asDB

Wanting to learn more about Mac's Image Events I am trying to learn how to save a file from one type into another type. For example, if I have a BMP image named foobar.bmp I wanted to learn how to save it out as foobar.jpg but in my handler I get an error:
Image Events got an error: Can’t get «class asDB» id (application
"Image Events") of image "foobar.bmp".
My code inside my handler:
tell application "Finder"
set directory to (choose folder with prompt "Please select a directory.")
set someImages to (files of folder directory where name extension is "bmp") as alias list
repeat with someImage in someImages
tell application "Image Events"
launch
set workingImage to open someImage
save workingImage as JPEG in directory
close workingImage
end tell
end repeat
end tell
I did test to see if the save may need the POSIX Path instead of the Alias Path with:
set directoryPosix to (POSIX path of directory)
and changed the save:
save workingImage as JPEG in directoryPosix
but I am still producing the same error and I dont understand why. The code works but just throws an error and after searching I am unable to find a resolution. I already know how to do this with Bash using ImageMagick and I could do this using AppleScript and SIPs but I would like to learn more about Image Events. What am I doing wrong to throw the error? If it helps my OS is up-to-date and running Yosemite version 10.10.5.
You need to specify the full (HFS or POSIX) path of the new file rather than the alias specifier to the destination folder:
set directory to (choose folder with prompt "Please select a directory.") as text
tell application "Finder" to set someImages to (files of folder directory where name extension is "bmp") as alias list
tell application "Image Events"
launch
repeat with someImage in someImages
set workingImage to open someImage
set fileName to name of workingImage
set newFilePath to directory & text 1 thru -4 of fileName & "jpg"
save workingImage as JPEG in newFilePath
close workingImage
end repeat
end tell

POSIX file works in tell block

The following works in Script Editor (or an Applescript App), but not in XCode:
tell application "Finder" to set folder_list to items of folder POSIX file "/Users"
Specifically, I get at runtime:
Finder got an error: Can’t make «class ocid» id «data optr000000002094230000600000» into type integer. (error -1700)
If I try "double coercion":
...((folder POSIX file "/Users") as POSIX file)
I get:
Can’t make «class cfol» «script» of application "Finder" into type POSIX file. (error -1700)
I did see something similar discussed here, but the solution did not work for me:
"POSIX file" works in Applescript Editor, not in XCode
Thanks!
//Reid
p.s. I know I could just use "Macintosh HD:Users"... This works, unless somebody renamed their hard drive.
Applescript has the "path to" command to find paths to well known folders, like a user's home folder, desktop, documents folder etc. That's the best way to access the folders. You can see all the locations applescript knows in the Standard Additions applescript dictionary for the "path to" command. It also knows where the users folder is. As such I would write your code like this...
set usersPath to path to users folder
tell application "Finder" to set folder_list to items of usersPath
You can try to smoothen it out yourself, as I suspect the AppleScript editor does for you:
tell application "Finder" to set folder_list to items of folder (POSIX file "/Users" as text)
What I did, was to coerce the posix file to text, otherwise it is of class furl which really isn't what Finder can take. I'd try to coerce your posix file statements to text, and see if that helps.
This compiles and runs, both from within my Editor, and from the script menu:
tell application "Xcode"
tell application "Finder"
set m to folder (POSIX file "/Users" as text)
set n to name of m
tell me to display alert n
end tell
end tell
I hope this helps.

AppleScript choose file or folder

can I use AppleScript to choose either file or folder in one time?
Now I could use
tell application "SystemUIServer" to return POSIX path of (choose file)
or
tell application "SystemUIServer" to return POSIX path of (choose folder)
to get file or folder. However I cannot get file or folder in one time.
No, you can't do it with "choose file" or "choose folder" verbs, but choosing a file or folder (or multiple files/folders) is supported by the underlying NSOpenPanel. So you can do it with AppleScriptObjC. Here's an example using ASObjCRunner (derived from here):
script chooseFilesOrFolders
tell current application's NSOpenPanel's openPanel()
setTitle_("Choose Files or Folders") -- window title, default is "Open"
setPrompt_("Choose") -- button name, default is "Open"
setCanChooseFiles_(true)
setCanChooseDirectories_(true)
setAllowsMultipleSelection_(true) -- remove if you only want a single file/folder
get its runModal() as integer -- show the panel
if result is current application's NSFileHandlingPanelCancelButton then error number -128 -- cancelled
return URLs() as list
end tell
end script
tell application "ASObjC Runner"
activate
run the script {chooseFilesOrFolders} with response
end tell
ASObjCRunner converts a NSArray of NSURL objects into an AppleScript list of files; the results can look something like:
{file "Macintosh HD:Users:nicholas:Desktop:fontconfig:", file "Macintosh HD:Users:nicholas:Desktop:form.pdf"}
Firstly, you don't need a tell for that.
POSIX path of (choose file)
Secondly, it is not clear why you need this. Do you mean you want to select a file and it's folder? That's not how you do it; you select the file then parse the file path for the containing folder or use one of the many methods to do that, like
set f to (choose file)
set posixF to POSIX path of f
tell application "Finder" to set filesDir to container of f as alias as text
set posixDir to POSIX path of filesDir
{f, posixF, filesDir, posixDir}
If you want to be able to select multiple folders and files at the same time, I don't think there is a "pure applescript" way to do this (aside from using a drag-drop aware script application).

Accessing folders in AppleScript

Sorry, complete beginner here with AppleScripting.
I'm trying to do a very simple thing, move files from one folder to another.
tell application "Finder"
set this_folder to "Users:chris.nicol:Movies:SmartConverter:"
set this_list to every file of this_folder
repeat with i in this_list
--if i does not start with "x" then
move i to "users:chris.nicol:music:itunes:itunes media:automatically add to itunes:"
--end if
end repeat
end tell
However I keep getting an error:
I've tried different commands (count, parentContainer, etc.) on the folder, but I get the same type of error. I've tried different formatting for the folder ...
Users/chris.nicol/Movies/SmartConverter/
Macintosh HD:Users:chris.nicol:Movies:SmartConverter
etc.
Any ideas on what I'm doing wrong?
Thanks in advance,
Chris
Simple tip... if you want to find the proper path you should use try this and look in the results field for the proper path. You'll see that the name of the hard drive, Macintosh HD, is required. Note you could use "choose file" as well if you wanted the path to a file.
(choose folder) as text
Next, the path that you will see is a string. Applescript sees it as a string not a path to a file or folder. As such, when you want to use the string as a path then you must put the word "file" or "folder" in front of it as appropriate to make applescript use it properly. Therefore your script should look like this. Note that the move command can handle a list of files so the repeat loop isn't needed.
set this_folder to "Macintosh HD:Users:chris.nicol:Movies:SmartConverter:"
tell application "Finder"
set this_list to every file of folder this_folder
move this_list to folder "Macintosh HD:Users:chris.nicol:music:itunes:itunes media:automatically add to itunes:"
end tell
Try:
set thisFolder to (path to movies folder as text) & "SmartConverter"
set thatFolder to (path to music folder as text) & "itunes:itunes media:automatically add to itunes"
tell application "Finder" to move files of folder thisFolder to thatFolder
You must have some invisible or otherwise uncopyable files in one of the folders. Either add something like without invisibles to the end of your set this_folder line, or get rid of your loop altogether and simply call
move files of entire contents of this_folder to (the Add Automatically folder)

Resources