find fileName in a folder using apple script - applescript

i would like to get the first file name of a folder using appleScript language
it should look like that:
Folder: photo.png, photo1.png, photo2.png, photo3.png
tell application "Finder"
set fileName to (1° file in this case photo.png)
end tell
please anyone than can help me out thanks

Almost.
tell application "Finder"
set fileName to name of first file of folder "Macintosh HD:path:to:folder"
end tell

Related

Using AppleScript to Select a File

I'm trying to get AppleScript to select a file, but I'm getting an error when I execute the script.
Here's the code
tell application "System Events"
set a to "/Users/me/files/"
set fileName to "myFile.jpg"
set thePath to POSIX path of a
tell application "Finder"
set selection to fileName of thePath
end tell
keystroke "c" using command down
end tell
I'm getting an error "Can’t get POSIX path of "/Users/me/files/"
Essentially, what I'm trying to do is find a way to select a file so that I can copy it for later. But I want to copy the actual file, not the path of the file. The idea is to create a service that copies the file so that I can paste it into another application easily.
If there's a better way to do this, then please let me know
These following 2 lines of code would copy your file to the clipboard. This would only work with a single file. Not multiple items.
activate
set the clipboard to POSIX file (POSIX path of (choose file))

Hiding all files in a user chosen directory

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?

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 move folder script

I am a complete applescript virgin and I have 2 versions of the same application that use different frameworks that have the same name.
I'm looking to create 2 scripts that move the correct version of the framework into the frameworks folder so I can swap them out quickly from the desktop.
This is what I have so far but it is throwing up an AppleEvent Handler failed -10000 error.
tell application "Finder"
duplicate POSIX file "Machintosh HD/Framework Store/Test.framework" to POSIX file "Machintosh HD/Library/Frameworks/Test.framework" with replacing
end tell
Can someone point out where I am going wrong?
Any help is greatly appreciated.
Just noticed you also had the Had disk name. The first forward slash "/" is all you need to refer to that.
tell application "Finder"
set rootFolder to POSIX file "/Framework Store/Test.framework"
set targetFolder to POSIX file "/Library/Frameworks/"
duplicate rootFolder to targetFolder with replacing
end tell
Try:
tell application "Finder"
duplicate POSIX file "Machintosh HD/Framework Store/Test.framework" to POSIX file "Machintosh HD/Library/Frameworks" with replacing
end tell

move a file in finder with applescript

I just want to move an image from one folder to the other, replacing the one that's already in there:
tell application "Finder"
copy file "/Users/xx/Documents/img.jpg" to folder "/Users/xx/Documents/State"
end tell
When I run it, I get an error message saying
Finder got an error: Can’t set folder [path] to file [path]"."number
-10006 from folder [path]
Please help me!
Try:
tell application "Finder"
duplicate POSIX file "/Users/xx/Documents/img.jpg" to POSIX file "/Users/xx/Documents/State" with replacing
end tell
Or
tell application "Finder"
move POSIX file "/Users/xx/Documents/img.jpg" to POSIX file "/Users/xx/Documents/State" with replacing
end tell
As #adayzdone notes, the error appears because you're using a Posix-style path without declaring it.
Another approach is to use colon-separated HFS paths, like so:
move file "Macintosh HD:Users:xx:Documents:img.jpg" ¬
to "Macintosh HD:Users:xx:Documents:State:" with replacing
With colon-separated paths you need to include the whole thing, including the volume name (I'm assuming Macintosh HD here), otherwise it'll throw our good friend error 10,006.
It helped me:
set theSource to POSIX file "/Users/xx/Documents/img.jpg"
set theDest to POSIX file "/Users/xx/Documents/State"
tell application "Finder"
move theSource to folder theDest with replacing
end tell

Resources