Automating excel in python on a mac - macos

I'm automating excel with python through the COM interface, with pywin32. I'd like to port my script to mac. Is there any chance of this happening? I realize that I can't use COM on a mac, and the xlutils python modules won't work (since I need to copy graphs, etc.). Is there anything else I can use?

Unfortunately development of appscript has been stopped,
I'm not aware the development of AppleScript has been stopped. What is your source on that?
As far as I can tell, AppleScript is still maintained and has a future and is working well.
Also there is a distinction between "AppleScript" the scripting language and the Inter-Application Communication Technology (IAC) called: "AppleEvents"
You can find the AppleScript Editor.app at the following location on your Mac:
/Applications/Utilities/AppleScript Editor.app
Open up the AppleScript Editor.app and then choose from the File menu: "Open Dictionary..."
Then select "Excel.app"
This will open the Apple Event dictionary for Excel. You will be able to see "Nouns" and "Verbs" and "Properties" that are supported in Excel for Apple Events.
AppleScript can be used to send commands to Excel via Apple Events, but so can other scripting languages such as Python.
If I were you, I would consider making an abstract class of Excel and two concrete subclasses, one that uses the Windows version of the code for Windows and anther that uses the Apple Event code that the Mac version of Excel might need.
See: the Wikipedia Article on Apple Events:
http://en.wikipedia.org/wiki/Apple_events
It's really Apple Events -- not AppleScript that you may want to use, but you could look at the following article too:
See Also: the Wikipedia Article on AppleScript:
http://en.wikipedia.org/wiki/AppleScript
--- edit ---
I believe that the misconception that Apple is lessening its support of AppleScript comes from the fact that AppleScript Studio was discontinued, but it was replaced with something just as good or better. Now, you can use AppleScript to develop full-fledged Mac applications in XCode. In addition, AppleScript can also still be used in Automator workflows, in XCode to build Automator actions, and in the AppleScript Editor, all of which are delivered by Apple in the latest version of OS X (Mountain Lion 10.8.4) and in the latest version of XCode (version 4.6.2)
So AppleScript is still a viable option, though the OP was asking about a Python solution. Apple Events are available from Python.

Currently the only possible solution would be appscript, which is a module that allows to write AppleScript macros in python (and ruby). Unfortunately development of appscript has been stopped, but for now it keeps working well enough.

Related

Plausibility of combining Kod and Komodo Edit?

I would like to know how difficult it would be to merge the well-designed Kod editor and the powerfully-built Komodo Edit together. I'd like to think it could turn into a native Mountain Lion programming editor with a simple interface (focus on elegance) and yet with powerful editing features.
Kod is a native OS X app, while Komodo is a cross-platform app based on Mozilla code. Both are under "open-source" licenses - Kod's license seems very permissive, and Komodo's is... long. I'm still deciphering it, but it seems like one could take code and put it into a new project.
I see this as becoming a new open source project: Kodmodo. I just don't know how compatible the source could be between the two programs.
Kod looks interesting but quite young. An editor without a preferences section for keybindings and color schemes? Might develop into an interesting project (node.js based). Quite a bit of Komodo Edit is done in javascript.
Alas, the main implementation language for Komodo is Python and the interfaces (opening files etc) are Mozilla. Kod uses OSX goodies and states on their main page that
Written from scratch with modern OS X 10.6 APIs providing maximum OS integration (empahsis added)
It would be near impossible to integrate OpenKomodo and Eric where both use python for development and macros and scintilla for the edit component. Komodo and Kod will never happen because they start from different places entirely.

Scheme instead of Applescript?

Can I use Scheme instead of Applescript on the Mac to automate apps such as iTunes, iCal, Mail, etc.?
There is no fundamental reason that you could not build another language to construct the messages (Apple Events) that are passed through the OS to other processes. Back in the days of Classic, the MPW shell had constructs for doing exactly that.
Apple provides Open Scripting Architecture to support this kind of thing.
But...
Wikipedia reports that there are Javascript, Perl, Python, Ruby, and Tcl tools that can generate Apple Events with the Javascript OSA component being best integrated and a better MacRuby interface coming.
The result might not feel very scheme-like.
If it’s just that scheme is awesome, you are probably out of luck. But there are other options. appscript currently supports Ruby, Python, and Objective-C.
Combined with the ASTranslate application also linked on the appscript page, you should be able to do anything using appscript that you can do using AppleScript. I’ve used it to import FileMaker databases into MySQL, both directly and using Django, and also to control iTunes.
If you’re inclined that way, it might be possible to port appscript to scheme.
Silly suggestion(?) but: Douglas Crockford built a scheme interpreter in JavaScript, and you can script the mac (using OSA) using javascript. Could this work?

is there a scripting solution for determining the default application path for a file on the Mac?

For a given extension, for example ".psd", I'd like to be able to determine the default application path for opening this file, for example "/Applications/Adobe Photoshop CS4.app".
I've looked into the Launch Services API, and there are clearly programmatic ways to get this information. Unfortunately for my particular scenario, only a scripting solution (Applescript or shell script) will do.
I've also looked at "lsregister -dump". It seems to be unwise to rely on parsing this information, since there are no guarantees as to the stability of the output format.
I've been solving this problem in the past with Creator Codes, but since Apple seems to be phasing them out since Snow Leopard I'm trying to eliminate dependence on Creator Codes.
thanks
Launch Services is the one and only place to get that information. You can write a scripting addition that will expose its functionality to AppleScript, but then you have to install that on whatever machine you plan to run on.
System Events does give you this in Leopard
alt text http://img.skitch.com/20091222-eessetxeqbai2mnwduygtm1cd5.png

Mac OS counterpart to Sysinternals and Powertoys

As a developer coming from a Windows background, I'd always find the different Powertoys and Sysinternals apps as invaluable tools in aiding programming. Is there something similar for a developer to watch for in the Mac world?
A lot of those types of tools are included with the Mac OS, and some are available separately from Apple.
There's no Mac equivalent of the registry - everything goes in the file system somewhere - and you can use fs_usage to watch that. In a terminal:
sudo fs_usage
Check out Instruments, part of the Apple developer tools distribution.
Top 10 DTrace scripts for Mac OS X is an article that describes ten really useful tools that are Mac equivalents of some of the Sysinternals tools such as ProcessMon, FileMon, etc.
The post's author created some of the scripts when he made the DTraceToolkit, which he says Apple then customized and enhanced for inclusion by default in Mac OS X.
Some script names look like they end with the word "noop," which might be a bit confusing--it's actually "snoop." As in spying on system activity.
Mac OS X comes with a lot of unix tools like sar, top, etc. What metrics are you looking for specifically?
Not as such. Some of the Powertoys implement features that are built-in to the OS, some are available as various utility programs.
As for Sysinternals, there are quite a lot of them. If you want specifics I'd need a better idea of what you use, but you could probably duplicate 90% of the functionality just from a terminal window. Learning how to use bash and the normal Unix utilities would be a very good skill to have.
http://newosxbook.com/index.php?page=downloads provide almost exact counterparts, albeit in CLI mode - with the upside that they also work on (jailbroken) iOS.

Change Sound (or other) System Preferences in Mac OS X

I'd like to be able to switch the sound output source in Mac OS X without any GUI interaction.
There are tools to do control the sound output, such as SoundSource and an applescript to open the preferences dialog.
What I am looking for is something that switches the preference instantly, like SoundSource but it has to be scriptable. The goal is to switch between my digital and analog output with one keystroke. I have a helper application that will launch a program or applescript on one keypress. All I need now is the applescript or application that switches the sound source quickly without any user interaction.
I'm willing to write some Objective-C if that is what it takes, but I'm pretty much a newbie at Cocoa development.
Do you have a one-click solution or can point me to a good tutorial on controlling sound system preferences from a Cocoa App or command line?
EDIT: I created a command-line application to do exactly this. You may download it at http://code.google.com/p/switchaudio-osx/downloads. Source code is available on the project site as well.
I created a command-line application to do exactly this.
You may download it at http://code.google.com/p/switchaudio-osx/downloads. Source code is available on the project site as well.
UPDATE (Dec. 2014): the code is now hosted on github -- https://github.com/deweller/switchaudio-osx. And works just fine in Yosemite.
Don’t think of it in terms of preferences; there’s no centralized system preference framework for this sort of thing. I believe what you need to do is use Core Audio to set the kAudioHardwarePropertyDefaultOutputDevice and kAudioHardwarePropertyDefaultSystemOutputDevice properties of the AudioSystemObject (using AudioHardwareSetProperty()).

Resources