Jump to content

Help modifying AppleScript to an Alfred action


Recommended Posts

I don't know much AppleScript and I'm trying to cobble together an action where I'd bring up a file in Alfred, Tab and select an action that would copy the selected file to the Desktop on another Mac on the network (it checks if the share is already mounted, and if not mounts it before copying the file over). I got the AppleScript to work in AppleScript Editor but am having trouble adapting it to work as an Alfred action.

 

Here's the AppleScript that works in AppleScript Editor:

 

-- Check the disk is mounted

set myDisk to "Macmini"

set mountedDisks to paragraphs of (do shell script "/bin/ls /Volumes")

 

if myDisk is not in mountedDisks then

mount volume "afp://Mac-mini.local/" & myDisk as user name "MYUSERNAME" with password "MYPASSWORD"

end if

 

tell application "Finder"

set theFile to item 1 of (get selection)

 

try

duplicate theFile to "Macmini:Desktop:" with replacing

on error errorMsg

display alert errorMsg

end try

 

end tell

 
 
 
For the Alfred action workflow I'm trying:
 

on alfred_script(q)

 

-- Check the disk is mounted

set myDisk to "Macmini"

set mountedDisks to paragraphs of (do shell script "/bin/ls /Volumes")

 

if myDisk is not in mountedDisks then

mount volume "afp://Mac-mini.local/" & myDisk as user name "MYUSERNAME" with password "MYPASSWORD"

end if

 

tell application "Finder"

try

duplicate q to "Macmini:Desktop:" with replacing

on error errorMsg

display alert errorMsg

end try

end tell

 

end alfred_script

 

 

 

 

I've tried it as /usr/bin/osascript and Run NSAppleScript without any luck.

 

Any help would be greatly appreciated.

 
Link to comment

 

I don't know much AppleScript and I'm trying to cobble together an action where I'd bring up a file in Alfred, Tab and select an action that would copy the selected file to the Desktop on another Mac on the network (it checks if the share is already mounted, and if not mounts it before copying the file over). I got the AppleScript to work in AppleScript Editor but am having trouble adapting it to work as an Alfred action.

 

Here's the AppleScript that works in AppleScript Editor:

 

-- Check the disk is mounted

set myDisk to "Macmini"

set mountedDisks to paragraphs of (do shell script "/bin/ls /Volumes")

 

if myDisk is not in mountedDisks then

mount volume "afp://Mac-mini.local/" & myDisk as user name "MYUSERNAME" with password "MYPASSWORD"

end if

 

tell application "Finder"

set theFile to item 1 of (get selection)

 

try

duplicate theFile to "Macmini:Desktop:" with replacing

on error errorMsg

display alert errorMsg

end try

 

end tell

 
 
 
For the Alfred action workflow I'm trying:
 

on alfred_script(q)

 

-- Check the disk is mounted

set myDisk to "Macmini"

set mountedDisks to paragraphs of (do shell script "/bin/ls /Volumes")

 

if myDisk is not in mountedDisks then

mount volume "afp://Mac-mini.local/" & myDisk as user name "MYUSERNAME" with password "MYPASSWORD"

end if

 

tell application "Finder"

try

duplicate q to "Macmini:Desktop:" with replacing

on error errorMsg

display alert errorMsg

end try

end tell

 

end alfred_script

 

 

 

 

I've tried it as /usr/bin/osascript and Run NSAppleScript without any luck.

 

Any help would be greatly appreciated.

 

 

It looks like you forgot to make q a file object; when you get the selection from Finder, it returns a file object automatically.

 

Maybe try changing this:

try
   duplicate q to "Macmini:Desktop:" with replacing

to this:

try
   duplicate ((POSIX file q) as alias) to ("Macmini:Desktop:" as alias) with replacing

I got this answer from Stack Overflow

 

Make sure you run this in the 'Run NSAppleScript' object; '/usr/bin/osascript' will not perform as expected.

 

Cheers :)

Edited by Tyler Eich
Link to comment

Thanks for the help. I tried that (and a few variations of it), but unfortunately it still wouldn't work.

 

It showed a message from Finder:

 

Finder got an error: Can't set "Macmini:Desktop:" to POSIX file "/Users/Jono/Desktop/test_file.png".

Edited by Jono
Link to comment

Thanks for the help. I tried that (and a few variations of it), but unfortunately it still wouldn't work.

 

It showed a message from Finder:

 

Finder got an error: Can't set "Macmini:Desktop:" to POSIX file "/Users/Jono/Desktop/test_file.png".

 

Oops! :( I updated my original response; see if that new code works for you

 

Cheers :)

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...