Get file name using drag-and-drop - applescript

Hi I have written applescript to get file name.
on open of thisPDF
set fName to thisPDF as string
(*
-
-
my code comes here.
-
-
*)
end open
When i drop any file on my application's icon. This code help me to get the file name in fName variable but this gives me the path of that file like
"MacHD:Users:my Name:Desktop:file Name"
But what I want is only file Name.
How to do this?

Give this a try:
set file_name to name of (info for alias "Macintosh HD:path:to:file.txt")

Related

Mac Automator - Compress (ZIP) a folder and rename as original folder name without the entirety of the path

Hi all, total newbie to any kind of programming, but I've written an Automator workflow to do a repetitive task I do daily. Basically I'm prepping artwork files and folders to send out to printers.
The steps are as follows:
Set Value of Variable (variable is ArtworkFolder)
where I drop the artwork folder containing the illustrator file and
PDF
Get Specified Finder Items
locates a file on a RAID server
Copy Finder Items (to ArtworkFolder)
copies said file into the folder
Ask for Confirmation -
prompts user to select an updated PDF
Ask for Finder Items -
opens the folder where the new PDF sits
Copy Finder Items (to ArtworkFolder)-
copies PDF file into the folder (overwriting older files)
Ask for Confirmation
prompts user to select print guidelines
Ask for Finder Items
opens the folder where the print guidelines sit
Copy Finder Items (to variable ArtworkFolder) -
copies print guidelines into the folder
THIS IS WHERE I THINK IT MAY BE GOING AWRY
Get Value of Variable (ArtworkFolder)
this is supposed to get the name of the original folder
Get Specified Finder Items
and all its contents
Create Archive
Then Zip the whole lot together
But it zips it with the name Archive.zip which I don't want so...
Rename Finder Items: Replace Text
which is set to find "Archive" in basename only ignoring Case. then
replace that with the variable ArtworkFolder
but it renames the folder with the entire path
I've tried to strip out the path by adding…
Rename Finder Items: Replace Text
which is set to find everything in the path basename only up to the
unique folder name and replace with nothing (eg I left the Replace box
blank)
But then it duplicates the folder name like this:
00000- Test Artwork Folder/00000- Test Artwork Folder.zip
All I want is to original folder name (00000- Test Artwork Folder) as
the zip file name.
Any tips would be greatly received!
For whatever reason, Automator doesn't have an included action to get file names, so you will need to use a third-party action or do it yourself. A Run AppleScript action can be used, but either way, note that you will need to save the workflow items and use the Ignore Input option as appropriate around getting the names, so you don't get them mixed in with the file items (also note that the workflow and action items are a list).
To get the name of a single item, something like the following can be inserted into your workflow, where the variable for the name can be used in the rename action:
Set Value of Variable { Variable: Original Input } -- stash current items
Run AppleScript: -- get the name
on run {input, parameters}
set {theContainer, theName, theExtension} to getNamePieces from (first item of input)
return theName
end run
to getNamePieces from somePath
tell application "System Events" to tell disk item (somePath as text)
set _container to path of container
set {_name, _extension} to {name, name extension}
end tell
if _extension is not "" then
set _name to text 1 thru -((count _extension) + 2) of _name -- just the name part
set _extension to "." & _extension
end if
return {_container, _name, _extension}
end getNamePieces
Set Value of Variable { Variable: Item Name }
Get Value of Variable { Variable: Original Input } (Ignore Input) -- continue workflow

Applescript Choose file, filename

What I'm trying to make is something like this:
set filename to (choose file)
display dialog "The name of the chosen file is: " & filename
But when I use that script it will return a path to the file something like .../.../.../.../nameoffile.jar. But what I want is that it will return the filename and not the path (so just only nameoffile.jar). Is there a way to do that?
Thanks for reading, please give an answer as soon as possible ;),
Jort
Use the Finder Suite:
set chosenfile to (choose file)
tell application "Finder"
display dialog "The name of the chosen file is: " & (name of chosenfile)
end tell

How to get the name and path from applescript

I have a file, say, "123.xyz", opened in TextWrangler.
How can I use Applescript to get the "its absolute path + full name" of the currently opened file.
So that I could use this in another program or script.
I modified some code I found online, but it didn't work for me.
tell application "TextWrangler"
tell document 1
-- Get (absolute path + filename) of the current file
set FileName to «class ppth» as text
end tell
end tell
Any help is highly appreciated.
tell application "TextWrangler"
--This will return the file path of document 1 in the following format: file "Macintosh HD:Users:UserName:Desktop:document1.txt"
set filePath1 to file of document 1
--This will convert the file path of document 1 to the following format: "Users/UserName/Desktop/document1.txt"
set filePath2 to POSIX path of filePath1
end tell
This SHOULD work:
tell document 1 to set myVeryOwnPath to path as text

capturing edited file name from duplicated file

I copy a a file to a new folder then rename it. Next I want to open it:-
set myNewFile to duplicate myFile to myNewLocation
set the name of myNewFile to myNewFileName
open myNewFile
The first 2 lines above work but the open command doesn't work because it can't find the file since I renamed it. I want to do something like:-
open myNewLocation & myNewFileName
myNewLocation is a valid path, myNewFileName is just a string, I have all the data I need but I can't work out how to construct a valid usable path from the two items.
Alternatively I could maybe change the name at the same time as I duplicate the file, before I save it to myNewFile. I've tried:-
set myNewFile to duplicate myFile to myNewLocation with name myNewFileName
and
set myNewFile to duplicate myFile to myNewLocation with properties {name:myNewFileName}
Neither works.
Try:
tell application "Finder" to set myFile to file "Mac OS X:Users:JD:Desktop:testStart.txt"
set myNewLocation to (path to desktop as text)
set myNewFileName to "testEnd.txt"
set myNewFile to duplicate myFile to myNewLocation
set the name of myNewFile to myNewFileName
tell application "Finder" to open file (myNewLocation & myNewFileName)

list subfolders using applescript

This is my first applescript. I thought I'd do something simple like navigating to a folder using a path and listing the subfolders...Unfortunately, I can't figure it out :-)
Here is what I've tried so far:
The first try:
tell application "Finder"
set the_folder to POSIX path of "Users:MyName:Doc"
log the_folder
set folder_list to every item of folder the_folder
log folder_list
end tell
This produces an error:
"Finder got an error: Can't get folder "/Users/MyName/Doc".
Could someone please:
1. Explain to me what I'm doing wrong.
2. Provide an example that works.
Thanks in advance.
btw the folder does exist on my machine...
UPDATE: Oops! It appears that I have given you the wrong information so I will give you the correct information.
The command POSIX path of requires a complete alias reference. By that I mean supplying the full file reference (i.e. <your_disk_name>:Users:<your_user_name>:somefolder:). Make sure that if you're referring to a folder that you end the reference with a colon (i.e. Macintosh HD:Users:). An improved version would look like this:
tell application "Finder"
set the_folder to (POSIX path of ("<your_disk_name>:Users:<your_user_name>:Doc:") as alias) as alias
set folder_list to every item of the_folder
end tell
OPTIONAL
To coerce a POSIX path (i.e. /Users/<your_user_name>/somefolder) back into an alias, two conversions are needed.
Conversion 1: The first step is to convert the reference into a file reference. To do this, place the words as POSIX file after the reference, like so:
"/Users/<your_user_name>/somefolder" as POSIX file
This code procudes a file reference in this form: file "<your_disk_name>:Users:<your_user_name>:somefolder:"
Conversion 2: Add a second coercion, as alias, to the end of the reference...
"/Users/<your_user_name>/somefolder" as POSIX file as alias
This code produces an actual alias reference: alias "<your_disk_name>:Users:<your_user_name>:somefolder:
If you have any questions, just ask. :)
Posix paths are paths you use at the command line and are "/" delimited. Applescript paths are ":" separated so just use those. Try this script to see what the path should look like...
set folderPath to (choose folder) as text

Resources