AppleScript to get name of single selected file in finder - applescript

I want to get the name of the currently in the finder window selected file. I'm working under OS X 10.9.2.
Here's my code:
display dialog "test"
tell application "Finder"
set theItems to selection
display dialog number of theItems
repeat with itemRef in theItems
display dialog name of itemRef
end repeat
end tell
In the Finder I selected only one mp3 file.
If I run the script then the dialogbox with "test" and the dialogbox with "1" is displayed correctly. But then I got the error message that the file cannot be converted into a type string.
I hope you can help me to fix this bug and I thank you in advance for your reply!

The name property of a file is already a text (or string) type. You are not properly parenthesizing the dialog statement.
display dialog (get name of itemRef)

file cannot be converted into a type string.
Then convert it to a string.
display dialog (name of itemRef) as string
I'd suggest you to use log instead of display dialog for debugging purpose.
For instance, log name of itemRef will return (in your "Events" and "Replies" window):
(name of document file 123.jpg of folder Desktop of
folder Saturnix of folder Users of startup disk)
As you can see, this is much more complex than a simple string of test (like "123.jpg"). This is telling you that name of itemRef is not returning the actual name of the file itemRef but a reference to that name. Luckily enough, calling as string on that reference will return us the actual name of the file.

Related

How can control the paste of a string with AppleScript?

In BBEdit and AppleScript I can loop through a string and set the string to the clipboard with:
set the clipboard to jsonString
I can then make a new text document and save it with:
set jsonParseFile to (name of project window 1) as text
save text document jsonParseFile to file ("some:location") without saving as stationery
set jsonParseFile to (name of active document of project window 1) as string
but when I try to paste the contents of the string with paste I get an error indicating that paste is not understood:
BBEdit got an error: active document doesn’t understand the “paste”
message.
So when I remove:
set jsonParseFile to (name of active document of project window 1) as string
and use:
paste of active document
I get the same error but when I just use paste I'm returned the error of:
BBEdit got an error: Can't continue paste.
How can I paste the string into the file variable jsonParseFile which is the front most file without calling on:
tell active document of project window 1 to paste
but rather with something like:
tell active document of file jsonParseFile to paste
that passes jsonParseFile? When I search I haven't found anything beyond keystroke and I'm not wanting to use and when I check the dictionary for answers I'm not getting much:
This is a simple example how to paste a string into the active document
set the clipboard to "Hello World"
tell application "BBEdit"
tell active document of project window 1
paste
end tell
end tell

Getting the Filename of a pdf print dialog in osx automator

I'm creating an automator pdf print plugin.
When you choose the print plugin the filename to the pdf is the input (normally /var/something /documentName.pdf)
I would like to get the documentName to use it later in an Rename Finder Item.
I'm using atm applescript to accomplish this.
on run {input, parameters}
tell application "Finder"
set fileName to name of ((POSIX file input) as alias)
end tell
return fileName as string
end run
The problem is that this only works when I put an Ask for Text Action before the applescript which displays the posix path.
If I remove the Ask for Text action the applescript fails.
The workflow is at https://www.dropbox.com/s/jp4t9pen3gvtyiq/Rename-Action.workflow.zip
I guess it is something simple but this is the first applescript / automator workflow I'm creating.
As I fail on commenting
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken post below.
Thanks!
I created a test workflow with this AppleScript:
on run {input, parameters}
tell app "System Events"
display dialog ((class of input) as string)
end
return input
end run
That displayed "list". I then modified it to:
on run {input, parameters}
tell application "System Events"
display dialog ((class of first item of input) as string)
end tell
return input
end run
That displayed "alias".
So, the input to a PDF workflow is a list of aliases. Write your script with that in mind and it should work. For example, this works:
on run {input, parameters}
tell application "System Events"
display dialog ((name of first item of input) as string)
end tell
return input
end run
When working with AppleScript, it can really help to forget everything you know about file paths. If you think in paths, you will be doing path-math in your head and all of it is unnecessary work. What you want to work with is objects. When doing file operations, you work with alias objects.
If you look at the PDF you are working with in Finder, and go File ▶ Make Alias then you’ll create an alias file. You can drag that alias file all around the file system of the disk it is on, put it in any folder, and when you open the alias file, it will still always open your original PDF, even if you forget what path name your original PDF file has, and even more importantly: the alias will open the PDF even if the PDF has moved to somewhere else in the file system. An alias does all that work for you. You don’t need to know the path names.
In AppleScript, rather than working with files, you work with aliases, and whatever you do to an alias is also done to the original file. So you don’t need to know the path name of a file to change its name — you only have to have an alias of it to work on. You store that alias in a variable.
So what you want to do is set the input PDF alias to a variable, and then later, that variable is what you give Finder to rename. You don’t have to know any paths. It doesn’t matter where the input PDF is stored on the file system — the alias will take care of that.
Here is an example AppleScript that demonstrates the principle of taking an alias as input, and then later renaming that alias (and thus, the original file:)
tell application "Finder"
set theInputFile to (choose file)
-- do a workflow here
set the name of theInputFile to "Renamed" & "." & the name extension of theInputFile
end tell
Here is a line-by-line description of the above script:
the opening tell block that specifies we are talking to Finder
show the user a choose file dialog box, and set the alias that is returned by that dialog box to a variable called “theInputFile”
a comment that is a placeholder for whatever workflow steps you might want to do
rename theInputFile to “Renamed” and its original file extension
quit talking to Finder
And even where you want to work with the folder that contains your original input file, or want to know what disk the input file is on, you still don’t need to work with path names:
tell application "Finder"
set theInputFile to (choose file)
set theContainingFolder to open the container of theInputFile
set theInputFileDisk to the disk of theInputFile
end tell
And if you want to know what kind of file the input file is, you don’t have to look at the filename extension and figure it out, you can just say:
set theInputFileKind to the kind of theInputFile
if theInputFileKind is equal to "Portable Document Format (PDF)" then
-- do stuff
end if
And if you want to work in specific folders, such as the home folder, there are special properties for that, like “the path to the home folder” so that the following script opens “~/Public/Drop Box” on any system, no matter what the user name:
tell application "Finder"
activate
set theHomeFolder to the path to the home folder as alias
set theDropBoxFolder to folder "Drop Box" of folder "Public" of theHomeFolder
open theDropBoxFolder
end tell
You can walk around disks and folder structures as objects as shown above, so again, there is no need to think in paths. Think in terms of setting variables to objects that you want to interact with.
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken's post
Thanks!

How to open a folder containing a specific string? (Mac)

We got some folders with project IDs with the following Pattern: x123_projectname.
I use Alfred in my workflow and I need to find and open a specific folder by the ID.
It is possible to run applescripts from Alfred. I'm new to applescript and google helped me to create this this:
set theString to "/path/to/folder/containing/projects/"
display dialog "What is the ID?" default answer ""
set theID to the text returned of
(display dialog "What is the ID?" default answer "")
tell application "Finder"
activate
set x to theString & "theID" as alias
open x
end tell
but it didn't work - do you have any hints for me?
You have a couple problems with your script but in general you have the right idea. The main problem is that applescript doesn't work with slash delimited paths. Applescript paths are colon delimited. You can convert from slash to colon delimited using the "posix file" command. Give this a try. Good luck.
set theString to "/path/to/folder/containing/projects/"
display dialog "What is the ID?" default answer ""
set theID to the text returned of result
set macString to POSIX file theString
tell application "Finder"
activate
set theResult to every folder of macString whose name contains theID
open theResult
end tell

AppleScript or Automator flow to import photos into iphoto and set album name as files folder name

I store photo files in folders on my computer before i add them into iphoto.
I want to add the contents of a selected to folder to iphoto and name the new album as the folder name
I have created an automator flow where I do the following
(Select folder initially to work)
Get selected finder items -- This is grabbing the folder
Set Value of Variable -- this is grabbing the full path name and setting a variable with the name
Get Folder contents -- this gets all the photos contained.
Import Files into iphoto -- This adds the photos into iphoto and creates a new album using the variable name.
The issue i have is the variable name sets the full path of the files,
/Users/Johnny/Photos/Dayout
Is the a script that can take just the name of the initial folder "Dayout"
Thanks in advance to anyone who can help
Cheers
John
activate application "SystemUIServer" -- http://www.openradar.me/9406282
tell application "Finder"
activate
repeat with f in (get selection as alias list)
set n to name of f
tell application "iPhoto"
if not (exists album n) then new album name n
import from f to album n
end tell
end repeat
end tell
There is a bug in 10.7 and 10.8 where Finder ignores new windows when getting the selection property. If you open a new Finder window, select some items, and run tell app "Finder" to selection in AppleScript Editor, the result is items selected in some window behind the frontmost window (or an empty list). One workaround is to move focus to another application and back.

IF Statesment using AppleScript

I'm trying to different events depending on what application is currently the "open" application - The one which is foremost of the screen. I have managed to save the name of the application using a variable. Using this code.
tell application "System Events"
item 1 of (get name of processes whose frontmost is true)
set openWindow to (get name of processes whose frontmost is true)
do shell script "echo " & openWindow & " > /Users/name/temp/currentWindow.txt"
end tell
I then tried to use this code do different events for each open application
tell application "System Events"
if openWindow = "AppleScript Editor" then
display dialog "my variable: " & openWindow
end if
end tell
However this code does not apper to do anything, I don't have any error messages or anything however the code doesn't display the dialog box. If I place the code for the dialog box in the first section of code it will display the name of the open application.
Any ideas on how to get this to work, it would be very helpful
To explain your problem, it's because of this code...
set openWindow to (get name of processes whose frontmost is true)
That code returns a list of items. Notice you asked for processes (plural), so you can get more than one so applescript gives it to you as a list whether there's one or more items found. What's strange is that in the line above this you do ask for "item 1" of the list but for some reason you don't do anything with that line of code. Normally I would write that line of code like this so I only get item 1...
set openWindow to name of first process whose frontmost is true
Anyway, you can't compare the string "AppleScript Editor" to a list {"AppleScript Editor"}. They are not equal so your if statement is never true.
Display dialog displays a string. So when you move that code outside the if statement, applescript is smart enough to convert your list into a string so it can be displayed.
So bottom line is you are getting a list and you must access the items of the list. The items are strings so get one (in your case you want item 1) and use that in the if statement.
Hopefully you can learn from this explanation. Good luck.
In the first script cast the openWindow variable to a string:
set openWindow to (get name of processes whose frontmost is true) as string

Resources