Jump to content

Selecting text on MAC, and putting this into a variable in applescript

Recommended Posts

I would like to select a filename and put this name into an applescript to reformat it into a hard-link that I would place in an obsidian document.

The script would then move the file to the external drive.


The link could open the external file from an obsidian note... something like: [BookXXX](<file:///Volumes/Drobo/linked_files/BookXXX>)


so far my test script works, but I have not figured out how to set var "file_name" from the selection coming from outside the script 



set file_name to "test_file"

display dialog "what do you what to call this link?" default answer "a_file"

set the var_ValueX to the text returned of the result

set var_link to "[" & var_ValueX & "]" & "(<file:///Volumes/Drobo/linked_files/" & file_name & ">)"

display dialog var_link

Link to comment

regarding the link part, you might want to review this script by @deanishe


to copy the file name (assuming you want to do this one file at a time) you could use

tell application "Finder" to set file_name to name of item 1 of (get selection)


Link to comment

Thanks. I tried the line you gave me above, but got compile errors... nevertheless, I did hack through this code that seems to work fine:


on alfred_script(q)

# Get selected file name from Finder
# ==================================
tell application "Finder"
    set theItems to selection
    --display dialog number of theItems
    repeat with itemRef in theItems
        --display dialog (get name of itemRef)
        set file_name to (get name of itemRef)
        --display dialog "file_name is " & file_name
    end repeat
end tell
# the selection is now in var: file_name
# ======================================
--display dialog "file_name is " & file_name
        set the var_ValueX to the file_name
        set var_link to "[" & var_ValueX & "]" & "(<file:///Volumes/Drobo/linked_files/" & file_name & ">)"
        set the clipboard to var_link
        --display dialog var_link

end alfred_script



My problem in the workflow now is to move the file from it's location on the MAC to my hard drive. I was trying to set a workflow variable

from my applescript so I could read it into a bash script that would move the file, but I got stuck there too.

Allthough I used the same name for the variable (1) in my applescript, and (2) in the Workflow Environment Variables,

the variable value is not set by the applescript. ??


Maybe there is another way to fet the filename and then move it to "Volumes/Drobo/linked_files" ??

Link to comment

Could you not do it a hacky way and just copy paste the file with some automated keystrokes? When triggered it will perform cmd+c, then open a finder location, then cmd+option+v to move. 

Apologies if I've misunderstood what you need though. if you wanted a more elegant way to copy/move files does this help?



Edited by Craigers
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...