xcode 4.6 applescript open text file for reading - xcode

I am trying to incorporate my applescript into xcode. the script works normally with applescript but not ran in xcode. I am trying to open the file for reading. here is the code
set Location to "US"
set DriverFile to "/Volumes/MacPrintDrivers/" & Location & "DriverInstall.txt"
set DriverInstallFile to POSIX file DriverFile
open for access DriverInstallFile
i have confirmed the file exists and i can display the contents. i can't seem to read this way in Xcode. the error i get is
«script» doesn’t understand the «event rdwropen» message. (error -1708)

ASOC (AppleScript Objective-C) has some problems with scripting addition commands (such as open for access). Sometimes you can work around this by saying tell current application to, e.g. tell current application to open for access. But for further details I suggest you get Shane Stanley's book: http://www.macosxautomation.com/applescript/apps/ He has explored this in great depth.

Related

How can I open multiple finder windows at the same time with apple script?

I'm trying to figure out how to open multiple finder windows at the same time; like opening several at a time.
Arbitrary example:
tell application "Finder" to open (every folder of home whose name starts with "Do")
See also How do I ask a good question?, and show your existing code.

MacOS Sierra & SMB & Synology NAS & Applescript crashes Finder

I've discovered a bug that I don't know how to fix. So I have an Applescript that tells the mac to highlight a certain file in an already opened Finder window connected to a shared folder on a Synology NAS server. Here's the Applescript:
set theFile to "West Office Files:_Scan Inbox:LAR:1002.pdf"
tell application "Finder"
reveal document file theFile
end tell
An error pops up saying that Finder got an error: document file "West Office Files:_Scan Inbox:LAR:1002.pdf" doesn't understand the "reveal" message. I have also tried using "select" instead of "reveal" and the problem persists. However, I just discovered it doesn't throw an error the first time the script is ran. But if I run it again to select a different file (but in same folder location) it then throws the error.
Then strange things begin to happen in Finder. Can't click on a finder window showing files, can't close any windows. Sometimes Finder will start popping up what I call "shadow boxes" which is the drop shadow of a window, but the window is invisible. Finder won't correct itself and the only fix I've found is to quickly log out the user to the Mac login screen and log back in. If I don't Finder will get worse and ultimately requires a force restart.
This is ONLY happening with MacOS Sierra (any sub version) and when connected to a Synology NAS server (any version DSM 6+) via SMB. AFP works fine, pre-Sierra Mac OSes work fine, and even when connected to an old Mac OS (Lion) Server via SMB or AFP there is no problem.
Anyone experiencing this? Any ideas for a solution?
Untested (im on mobile) but you need to transform osx path to posix file:
set theFile to POSIX file (POSIX path of "West Office Files:_Scan Inbox:LAR:1002.pdf")
tell application "Finder" to reveal theFile
With help from Pat_Morita I found a solution to the problem. Am posting it here to help others who use Synology NAS servers and Macs.
set theFile to POSIX file (POSIX path of "/Volumes/West Office Files/_Scan Inbox/LAR/1002.pdf") as alias
tell application "Finder"
set selection to {}
select theFile
end tell
Where the error was occurring was specifically when the AppleScript would tell the Finder to highlight a file and there was a file already highlighted. In MacOS Sierra it would throw an error in the Apple Event right when it moves away from the currently selected file. So adding in the "set selection to {}" has Finder deselect what is highlighted before selecting the next file. I also went with using POSIX for the file path by recommendation from this forum.
UPDATE ---
False alarm on finding a fix, well... sorta.
The error is still thrown and seems to only be happening when a Finder window is in Cover Flow view. All the other views work fine. Also, I discovered the bug is happening when MacOS Sierra is connected via AFP as well, not just SMB. I changed the AppleScript a bit:
set theFile to "West Office Files:_Scan Inbox:LAR:1002.pdf" as alias
tell application "Finder"
set selection to {}
select file theFile
end tell
Learned that Finder doesn't really care if the file path is set up via POSIX or not. Also added "file" to the select line.

exploring avaliable commands in an applescript program

I have an application and I can do the following command on (I know this because I googled for it):
tell app "TextMate" to reload bundles
What I would really like is to be able to ask the "TextMate" program:
tell app "TextMate" to list all commands
and have it list out all the things I can ask it to do:
... 'reload bundles', 'exit', 'open files'...
is there a way to do that with applescript?
The way to find all the commands of an app is to open its dictionary in your script editor. Usually "Open Dictionary ... " in the File menu, or drop the application onto the script editor.
[EDIT]
For applications that have AppleScript support, you can actually script opening the app itself with the Script Editor, a la:
set pathToApp to (choose file of type "APPL")
tell application "Script Editor"
open pathToApp
end tell
BUT this will be problematic with a non-scriptable app. You'll get an error, but Script Editor will actually open some part of the app (and it will be slow about it), then give you an unusable document. There's no way to catch this error. If you use the Smile script editor, you can use this method ...
set p to (choose file of type "APPL")
try
OpenDictionary(alias (p as string))
on error e
end try
... to open the dictionary of an app, and if it doesn't work (if the app doesn't have a dictionary), it returns an error but doesn't do anything else (but again, you can't catch the error and not have it complain, without hacking Smile)
[EDIT 2]
A rabbit hole to go down is trying System Events or the Finder to check for boolean of has scripting terminology property of a process, but I don't recommend it because I haven't found it to be reliable.
[EDIT 3]
Ach! I knew there was another method, but forgot what it was. As #mklement0 points out (thank you), you can do this to check for an app's script-ability prior to opening the app in Script Editor:
set pathToApp to (choose file of type "APPL") as text
set isScriptable to false
try
class of application pathToApp
-- only AppleScriptable applications have a class property
set isScriptable to true
end try
isScriptable

Create AppleScript for a program that isn't installed on the current computer

I'm trying to make two copies of an AppleScript, one that works for Entourage and one for out Outlook. I only have Entourage installed on the current computer.
According to the info on Microsoft's site, both applications have the same library of AppleScript commands, and I should be able to simply change the application name referenced within the script.
Changing:
Tell application "Microsoft Entourage"
to
Tell application "Microsoft Outlook"
Prevents me from saving the script because I don't have outlook installed on this computer. Is there any way around this? Do I need to use a text editor to edit the actual script file and change it there?
Thanks!
The following work-around may do the trick. On the computer where Entourage is installed, a using terms directive will let you compile the script, even if Outlook is not installed:
set theApp to a reference to application "Microsoft Outlook"
using terms from application "Microsoft Entourage"
tell theApp
get version
...
end tell
end using terms from
Upon compiling and saving the script the AppleScript Editor will bug you about the missing Outlook application, but it will nevertheless produce a compiled AppleScript file (.scpt).
Applescript is a pre-complied file format, meaning that every time you click "Save" it runs through a series of steps to ensure the script will work, but just short of actually running through the script's logic. Part of those steps is to look for the application to see if it exists on the Mac.
In short, if you want to save the script as an Applescript, you need the target application installed, otherwise you can save the script as a text file and move the file over to the target Mac to save as an Applescript over there.
It should be possible to make one script that works with both Entourage and Outlook, without bugging you if one isn't found either when you compile or when you run. I don't have either Entourage or Outlook but it should work like this:
using terms from application "Microsoft Entourage"
script theScript
tell application "Finder" to try
set theApp to application file id "Entourage's Bundle ID" as text
on error
set theApp to application file id "Outlook's Bundle ID" as text
end try
tell application theApp
-- do stuff
end tell
end script
end using terms from
store script theScript in "MyScript.scpt"
"using terms from" is only relevant when compiling the script - it isn't needed when running, though for some reason you'll still get bugged if that app isn't found. So by wrapping it around a script object and then writing out that script to file, the resultant script will still run but won't contain "using terms from" and so won't bug the user.
For getting a reference to the right app, Finder can look for it by ID and simply error if it isn't found rather than bugging the user. You'll need to insert the proper ID's there, I don't know what they are.

AppleScript Transmit Script for uploading file to replace itself on web server

So I have this idea for a handy little AppleScript which in my opinion would be very handy in speeding up the process of uploading a local file, to its same location on the server.
In other words, you have to specify the home folder on the server and locally, but once that's finished, it would be nice to just press like "Command" + "Shift" "U" for upload or some other hot key combination not in use by OS X for uploading the currently selected file in the Finder.
I find myself needing to do this a lot, and it will save a lot of time!
Someone please tell me if there is an easier way to do this, but I think this will be a good learning experience on top of it all.
I need some help on how I should get started however..
1) the command line program curl can upload files. 2) if you have a file selected in a Finder window applescript can get the selection. Using those 2 ideas you can automate your task. I don't know the exact curl command but that should be easy to find using google. So you select a file in the Finder and then run the script. The script can be run with a keyboard shortcut as you mentioned or just put it in the Script menu and run it from there.
tell application "Finder"
set selectedFile to item 1 of (get selection)
set selectedFile to selectedFile as text
end tell
do shell script "curl -switchesToUpload " & quoted form of POSIX path of selectedFile
I use Cyberduck which is an ftp client you can set it up so when you double click a file on the server it opens it up in your favorite editor.( textmate is my favorite.) it atuomagiclly downloads and uploads when you save.
this seems like a much better solution to the problem

Resources