Ivan -Oats- Storck
Ivan -Oats- Storck

Reputation: 4326

Sublime Text accessing view file name error

I am new to Python, and Sublime Text plugin development, and I don't know what I'm doing wrong here. I am using Sublime Text 3. I'm trying to create a plugin that will copy the file name to the clipboard. Can anyone help me understand this python error and/or offer a solution?

import sublime, sublime_plugin

class Filename_to_clipboardCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    sublime.set_clipboard(sublime.View.file_name())
    sublime.message_dialog("The full file path was copied to the clipboard")

and the error, when I call the plugin from the console, is:

>>> view.run_command('filename_to_clipboard')
Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 549, in run_
return self.run(edit)
  File "/Users/ivan/Library/Application Support/Sublime Text 3/Packages/Filename_to_clipboard/filename_to_clipboard.py", line 5, in run
sublime.set_clipboard(sublime.View.file_name())
TypeError: file_name() missing 1 required positional argument: 'self'

When I do:

sublime.set_clipboard(view.file_name())

from the conosole, it works! Why?

Upvotes: 0

Views: 1516

Answers (3)

MattDMo
MattDMo

Reputation: 102842

I hate to see all your effort wasted, but this functionality already exists in ST3. If you right-click in the edit area, Copy File Path is one of the options on the context menu. To create a keyboard shortcut, open Preferences -> Key Bindings - User and add the following item:

{ "keys": ["super+i"], "command": "copy_path" }

You can of course change the key binding to whatever you want. If the key bindings file is empty when you open it, just add opening and closing square brackets:

[
    { "keys": ["super+i"], "command": "copy_path" }
]

However, I still encourage you to learn Python and plugin programming, they're both quite rewarding and great fun! Good luck!


EDIT

Based on a comment by @skuroda, here's how I discovered the command to use:

I already knew there was a Copy File Path option in the context menu, but looking through Preferences -> Key Bindings - Default I couldn't find any shortcuts or macros already assigned to that action, and I didn't know exactly what the command's name was. So, I hit Ctrl` to open the console, then ran

sublime.log_commands(True) 

to have all actions logged to the console. I then right-clicked and selected Copy File Path, and

command: copy_path 

showed up, along with a message regarding the mouse's location when the context menu event occurred. I assigned the key combination, ran it, and it worked. To finish up, I run

sublime.log_commands(False) 

so the console doesn't get clogged with unnecessary info, then hit Ctrl` again to close the console.

Upvotes: 1

skuroda
skuroda

Reputation: 19744

Try self.view.file_name() rather than sublime.View.file_name(). You have a reference to an instance of the view for your TextCommand. It was written for ST2, but you may want to take a look at this tutorial

http://net.tutsplus.com/tutorials/python-tutorials/how-to-create-a-sublime-text-2-plugin/

Upvotes: 2

jamesadney
jamesadney

Reputation: 275

I don't know anything about the Sublime Text API, but it looks like your problem here is that file_name() is an instance method, and sublime.View is a class so sublime.View.file_name() fails.

In Python, when you call an instance method you are implicitly passing the instance as the first argument, which is why it says you are missing the argument self. You could even write the call explicitly. For example, view.file_name() could also be written sublime.View.file_name(view), but that's kind of silly.

Good luck with your plugin :)

Upvotes: 1

Related Questions